HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我
Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly nn beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 1E9+7.
Note: The necklace is a single string, {not a circle}.
InputThe first line of the input contains an integer T(1≤T≤10000), denoting the number of test cases.
For each test case, there is a single line containing an integer n(2≤n≤10^18), denoting the number of beads on the necklace.OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 1E9+7.Sample Input
2
2
3
Sample Output
3
4 大意是:一个串(不结环)由红色和蓝色珠子组成,要求每素数个串红色的珠子数量大于等于蓝色的,给定串的长度,询问问能构成的串的种类数,mod 1e9+7。
思路:
红用A表示,蓝用B表示
显然当n==2:
有 AB,AA,BA三种情况
记a[n],b[n],c[n]分别为以三种为末尾的字符串的个数
当n==3时:
AB后面可以加A ==> ABA (末尾为BA,下同)
AA后面可以加 A或B ==> AAA,AAB
BA后面可以加A ==> BAA
得到递推式
a[n] = b[n-1]
b[n] = b[n-1]+c[n-1]
c[n] = a[n-1]
记题目所求的个数为p[n]
p[n] = a[n]+b[n]+c[n] = 2*b[n-1]+a[n-1]+c[n-1] = (a[n-1]+b[n-1]+c[n-1]) + b[n-1] = p[n-1] + (b[n-2]+c[n-2]) = p[n-1] + (b[n-3]+c[n-3]+a[n-3]) = p[n-1]+p[n-3]
到此得到递推关系式:p[n] = p[n-1]+p[n-3]
变成矩阵快速幂的模版题了。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
#define pi acos(-1.0)
#define Max_N 1001
#define inf 0x3f3f3f3f
#define N 1001
#define ll long long
using namespace std;
const LL MOD = 1e9+;
struct mat{
LL a[][];
};
mat mat_mul(mat x,mat y,int len){
mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < len ; i ++){
for(int j = ; j < len ;j ++){
for(int k = ; k < len ;k ++)
res.a[i][j] = (res.a[i][j] + (x.a[i][k]*y.a[k][j]*1LL)%MOD)%MOD;
}
}
return res;
}
mat mat_qpow(mat a,LL b,int len){
mat ans;
memset(ans.a,,sizeof(ans.a));
for(int i = ;i < len ; i ++) ans.a[i][i] = 1LL;
while(b){
if(b&) ans = mat_mul(ans,a,len);
a = mat_mul(a,a,len);
b>>=;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{
LL n;
scanf("%lld",&n);
if(n == ){
puts("");continue;
}
else if(n == ){
puts("");continue;
}
else if(n == ){
puts("");continue;
}
mat b;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b = mat_qpow(b,n-,);
LL ans = (b.a[][]*6LL+b.a[][]*4LL+b.a[][]*3LL)%MOD;
cout<<ans<<endl;
}
return ;
}
/*
2
3
4
*/
HDU6030 Happy Necklace(递推+矩阵快速幂)的更多相关文章
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
- LightOJ 1244 - Tiles 猜递推+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
随机推荐
- sqluldr2 学习心得
前言 最近正在做一个项目,需要导出数据库中的表,单数数据库中有很多带有虚拟列的表,而且表中的数据非常的庞大,有些表中的数据上亿条,项目经理让我研究如何快速导出这些数据. 下面是我研究的一些经历: (1 ...
- nginx的白名单
为nginx设置白名单的几个步骤: 第一步:指定能访问的白名单 vim /etc/nginx/ip.conf (如果在公司,记得这里是外网IP,要不然测很久都不知道为什么不行) ; 第二步 ...
- zabbix性能优化记
以前用zabbix,都是简简单单的把服务端.客户端一搭,模板监控项一弄就完了.没怎么真正去弄过优化啊之类的.现在有个场景就是获取zabbix的数据贼慢,得对zabbix进行优化,开始动手分析一下. ...
- Fabric实例
Fabric的官网 http://fabric-chs.readthedocs.io/zh_CN/chs/index.html 参考<Python自动化运维 技术与最佳实践> 1:查 ...
- 初探JavaScript的截屏实现
最近参与了网易炉石盒子的相关页面开发,在做卡组分享页(地址:炉石盒子卡组分享),有个需求:用户可以把这个卡组以图片的形式分享给好友.最初的的做法是使用服务器把该页面转换成图片,然后把图片地址返回给前端 ...
- react 在 componentWillMount() 中调用异步函数时,componentWillMount() finishes after render()
刚开始使用 react,很多属性.方法不是很熟.在此记录下我所遇到的问题及解决方法. 我在 componentWillMount() 中调用了一个异步函数,在返回结果中调用 this.setState ...
- mysqlbinlog基于时间点恢复
基于时间点恢复 /data/mysq/mysqlbin.000026 #mysqlbinlog文件,恢复如下内容: 注意:按照时间点恢复时,可能同一个时间点有其他的操作,要结合上下文的时间选取~ # ...
- Kubernetes Service Account如何生成Token
Service Account是运行pods用到的帐号,默认是default.如果apiserver启动配置--admission-control=ServiceAccount,Service Acc ...
- 20175227张雪莹 2018-2019-2 《Java程序设计》第三周学习总结
20175227张雪莹 2018-2019-2 <Java程序设计>第三周学习总结 教材学习内容总结 (仅在此列举个性化学习总结) 一.编程语言的几个发展阶段. 1.面向机器语言:汇编语言 ...
- Python发送微信消息
针对此 需要安装itchat第三方模块 采用pip安装就可以了 pip install itchat import itchatimport time#引入时间函数进行测试time_format= ...