Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 863 Accepted Submission(s): 383Problem DescriptionLittle Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
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 n 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 109+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≤1018), denoting the number of beads on the necklace.
OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 109+7.
Sample Input2 2 3
Sample Output3 4
Source
【题目大意】用红蓝两种珠子,串成一条项链,
使得任意素数长度的区间都有红色珠子大于等于蓝色珠子,问有多少种串法
【思路】考虑最小的素数2,任意素数长度区间 L = 2k + 1, 因此只要任意长度为2和3的区间满足条件即可
假设 1 = 红, 0 = 蓝
易得 F( 2 ) = 3,
F(3) = 4
要求长度为n的串,可以在n-1的串后加1 或 0,
加 1 肯定满足要求
加 0 要满足 n-1 串的末尾不能是0,且倒数第二位也不能是0,因此在n-3串后直接加110
得递推式 F(n) = F(n-1) + F(n-3)
-------------------------------------------------------------------------
再用 矩阵快速幂 加速求递推式
![](https://i-blog.csdnimg.cn/blog_migrate/063a063ca81289538bcab2e23c968d2a.png)
求得 A =
【AC代码】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/* 矩阵快速幂模板 by chsobin */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int maxn = 3; const ll mod = 1e9 + 7; //矩阵结构体 struct Matrix{ ll a[maxn][maxn]; void init(){ //初始化为单位矩阵 memset(a, 0, sizeof(a)); for(int i=0;i<maxn;++i){ a[i][i] = 1; } } }; //矩阵乘法 Matrix mul(Matrix a, Matrix b){ Matrix ans; for(int i=0;i<maxn;++i){ for(int j=0;j<maxn;++j){ ans.a[i][j] = 0; for(int k=0;k<maxn;++k){ ans.a[i][j] += a.a[i][k] * b.a[k][j]; ans.a[i][j] %= mod; } } } return ans; } //矩阵快速幂 Matrix qpow(Matrix a, ll n){ Matrix ans; ans.init(); while(n){ if(n&1) ans = mul(ans, a); a = mul(a, a); n /= 2; } return ans; } ////for debug //void output(Matrix a){ // for(int i=0;i<maxn;++i){ // for(int j=0;j<maxn;++j){ // cout << a.a[i][j] << " "; // } // cout << endl; // } //} int main(){ Matrix A = { 1,1,0,0,0,1,1,0,0 }; //递推矩阵 int t; cin >> t; ll n; int ans[5] = {0, 0, 3, 4, 6}; //枚举初始情况 while(t--){ cin >> n; if(n<=4) cout << ans[n] << endl; else{ Matrix m = qpow(A , n-4); ll temp = 6 * m.a[0][0] % mod; temp = (temp + 4 * m.a[1][0]%mod)%mod; temp = (temp + 3 * m.a[2][0]%mod)%mod; cout << temp << endl; } } return 0; }
【总结】:
看到数据量就明白了这一类题的做法
1.打表
2.根据打表结果,写出递推方程。
3.将递推方程构造为矩阵。
4.快速幂求解。