Not 010 strings
Base time limit: 1 seconds space limit: 131072 KB score: 80 If a 01 string satisfies a substring that does not exist 010, it is called a non-010 string. The number of non-010 strings of length n is calculated. (For 1e9+7) Input
A number n, which represents the length. (N<1E15)
Output
The number of non-010 strings with a length of N. (For 1e9+7)
Input example
3
Output example
7
Explanation: 000001011100101110111
After reading the question, such a topic must be able to find the law, otherwise the data is too big can not calculate at all. Assuming that the given length is n, the answer is f (n), then assuming that the last one is 0, 010 of the preceding may have F (n-1), the same assumption that the last one is 1, 010 of the preceding may also have F (n-1), and so the exclusion of the words there is still a problem, That is, the last 0 of the time may appear in front is 01 and make up 010, so add repetition. So assuming that the previous bit is 1, minus F (n-2), and of course, it might be 11 and make up 110 instead of 010, so we have to add the extra minus again, that is, add an F (n-3), so you can launch a formula F (n) =2*f (n-1)-F (n-2) +f (n-3). See this formula, the data is so large, so we use the matrix fast power to deal with the results can be quickly obtained.
Here is the AC code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const Long LongMod=1000000007;structmatrix{Long Longa[3][3];}; Matrix cal (Matrix A,matrix B) {inti,j,k; Matrix C; for(i=0;i<3; i++) { for(j=0;j<3; j + +) {C.a[i][j]=0; for(k=0;k<3; k++) {C.a[i][j]= (c.a[i][j]+ (a.a[i][k]*b.a[k][j])%mod+mod)%MoD; } } } returnC;}int out(Matrix A,matrix B) {cout<<"S:"<<Endl; cout<<a.a[0][0]<<" "<<a.a[0][1]<<" "<<a.a[0][2]<<Endl; cout<<a.a[1][0]<<" "<<a.a[1][1]<<" "<<a.a[1][2]<<Endl; cout<<a.a[2][0]<<" "<<a.a[2][1]<<" "<<a.a[2][2]<<Endl; cout<<"Base:"<<Endl; cout<<b.a[0][0]<<" "<<b.a[0][1]<<" "<<b.a[0][2]<<Endl; cout<<b.a[1][0]<<" "<<b.a[1][1]<<" "<<b.a[1][2]<<Endl; cout<<b.a[2][0]<<" "<<b.a[2][1]<<" "<<b.a[2][2]<<Endl; return 0;} Matrix Pow_mod (Long Longx) {Matrix S,Base; Base. a[0][0]=2; Base. a[1][0]=-1; Base. a[2][0]=1; Base. a[0][1]=1; Base. a[1][1]=Base. a[2][1]=0; Base. a[1][2]=1; Base. a[0][2]=Base. a[2][2]=0; s.a[0][0]=7; s.a[0][1]=4; s.a[0][2]=2; s.a[1][0]=s.a[1][1]=s.a[1][2]=0; s.a[2][0]=s.a[2][1]=s.a[2][2]=0; out(S,Base); while(x) {if(x&1) s=cal (s),Base); Base=cal (Base,Base); out(S,Base); X>>=1; } returns;}intMain () {Long LongN; Long Longans; while(SCANF ("%lld", &n)! =EOF) { if(n==0) cout<<0<<Endl; Else if(n==1) cout<<2<<Endl; Else if(n==2) cout<<4<<Endl; Else if(n==3) cout<<7<<Endl; Else{Matrix T=pow_mod (n3); Ans=t.a[0][0]%MoD; cout<<ans<<Endl; /*cout<<t.a[0][1]%mod<<endl; cout<<t.a[1][0]%mod<<endl; cout<<t.a[1][1]%mod<<endl;*/ } } return 0;}
51nod algorithm Marathon B non 010 series matrix fast Power