Happy Necklace
Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1146 Accepted Submission (s): 491
Problem Descriptionlittle Q wants to buy a necklace for his girlfriend. Necklaces is single strings composed of multiple red and blue beads.
Little Q desperately wants to impress him girlfriend, he knows that she'll like the necklace only if for every prime Len Gth continuous subsequence in the necklace, the number of the red beads are not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly beads. He wants to know the number of different necklaces, can make his girlfriend happy. Please write a Little Q. Since the answer is very large, please print the answer modulo.
Note:the necklace is a single string, {not a circle}.
Inputthe first line of the input contains an integer, denoting the number of the test cases.
For each test case, there was a single line containing an integer, denoting the number of beads on the necklace.
Outputfor each test case, print a single line containing a single integer, denoting the answer modulo.
Sample Input
223
Sample Output
34
SOURCE2017 Chinese College Student Program design Contest-Girls ' special title Description: You have a necklace with n colors of red or blue beads, you can cut the necklace into a length of a number of beads, if the amount of red beads intercepted more than blue beads, it becomes good, Ask how many good possibilities there are. N=2,res=3;n=3,res=4;n=4,res=6;n=5,res=9 can be found, so there are recursive an=an-1+an-3;
If you use a to indicate red, b for blue. Test instructions can clearly see that only a continuous sequence of lengths 2 and 3 is compatible!
If you end with B, then the next must be a, or add a AAB!
So the same can be the introduction of recursive an=an-1+an-3; Take a look at the data range of the topic, N Max 1e18, so the recursion of the General O (n) is obviously not feasible, so the matrix of the direct O (Logn) is quickly idempotent.
It is found that recursion is a four-order recursive type, so the resulting constant matrix should be of the dimension. Then simply bring the matrix to the Quick power template.
First look at this characteristic equation f[i] = f[i-1] + f[i-3], then there is a matrix as follows
Our goal matrix is to
So, how do we transpose this matrix?
First look at the target matrix: f[i]
F[i] = F[i-1] + f[i-3]
So, by multiplying the Matrix, transpose the first line of the matrix, it seems to be fixed: 1 0 1
Similarly, two or three lines are 1 0 0 and 0 1 0
The entire matrix is as follows:
#include <iostream>#include<algorithm>#include<cmath>#include<cstdio>#include<cstring>#defineINF 0x3f3f3f3f#defineMoD 1000000007using namespacestd; typedefLong Longll; Const intMAXN =100010; ll N; structMatrix {ll a[5][5]; }; Matrix Mul (Matrix x, Matrix y) {matrix temp; for(inti =1; I <=3; i++) for(intj =1; J <=3; J + +) Temp.a[i][j] =0; for(inti =1; I <=3; i++) { for(intj =1; J <=3; J + +) {ll sum=0; for(intK =1; K <=3; k++) {sum= (sum + x.a[i][k] * y.a[k][j]% MoD)%MoD; } Temp.a[i][j]=sum; } } returntemp; } Matrix Quickpow (Matrix A,ll k) {Matrix res; res.a[1][1] =1; res.a[1][2] =0; res.a[1][3] =0; res.a[2][1] =0; res.a[2][2] =1; res.a[2][3] =0; res.a[3][1] =0; res.a[3][2] =0; res.a[3][3] =1; while(k) {if(K &1) res =Mul (res, A); A=Mul (A, a); K>>=1; } returnRes; } intMain () {intT; scanf ("%d", &t); while(t--) {scanf ("%lld", &N); if(n = =2) {printf ("3\n"); Continue; } Matrix A; a.a[1][1] =1; a.a[1][2] =0; a.a[1][3] =1; a.a[2][1] =1; a.a[2][2] =0; a.a[2][3] =0; a.a[3][1] =0; a.a[3][2] =1; a.a[3][3] =0; Matrix Res= Quickpow (A, N-2); ll x= (res.a[1][1] + res.a[1][2] + res.a[1][3]) %MoD; ll y= (res.a[2][1] + res.a[2][2] + res.a[2][3]) %MoD; LL Z= (res.a[3][1] + res.a[3][2] + res.a[3][3]) %MoD; printf ("%lld\n", (x + y + z)%MoD); } }
2017 Chinese college Student Program design Contest-girls ' special Happy necklace (recursive + matrix fast Power)