[Luogu 1939] [TEMPLATE] matrix acceleration (series), luogu1939
Description
A [1] = a [2] = a [3] = 1
A [x] = a [X-3] + a [x-1] (x> 3)
Returns the remainder value of the nth clause of Series a on 1000000007 (10 ^ 9 + 7.
Input/Output Format
Input Format:
The first line is an integer T, indicating the number of queries.
The following T rows have a positive integer n in each row.
Output Format:
Each row outputs a non-negative integer to indicate the answer.
Input and Output sample input sample #1:
36810
Output sample #1:
4919
Description
For 30% of Data n <= 100;
For 60% of data, n <= 2*10 ^ 7;
For 100% of data T <= 100, n <= 2*10 ^ 9;
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define ll long long 6 #define Mod 1000000007 7 using namespace std; 8 int t;ll n; 9 struct matrix{ll m[5][5];}a;10 matrix multiply(matrix a,matrix b){11 matrix t;12 for(int i=1;i<=3;i++)13 for(int j=1;j<=3;j++){14 t.m[i][j]=0;15 for(int k=1;k<=3;k++)16 t.m[i][j]=(t.m[i][j]+a.m[i][k]*b.m[k][j])%Mod;17 }18 return t;19 }20 matrix fast(matrix a,ll k){21 matrix ans=a;k--;22 while(k>0){23 if(k&1) ans=multiply(ans,a);24 a=multiply(a,a);25 k>>=1;26 }27 return ans;28 }29 int main(){30 a.m[1][3]=a.m[2][1]=a.m[3][2]=a.m[3][3]=1;31 scanf("%d",&t);32 for(int i=1;i<=t;i++){33 scanf("%lld",&n);34 matrix tmp=fast(a,n);35 printf("%d\n",tmp.m[3][2]);36 }37 return 0;38 }