Description
a[1]=a[2]=a[3]=1
a[x]=a[x-3]+a[x-1] (x>3)
求a数列的第n项对1000000007(109+7)取余的值。
Input
第一行一个整数T,表示询问个数。
以下T行,每行一个正整数n。
Output
每行输出一个非负整数表示答案
Sample Input
3
6
8
10
Sample Output
4
9
19
HINT
对于30%的数据 n<=100;
对于60%的数据 n<=2*10^7;
对于100%的数据 T<=100,n<=2*10^9;
#include<iostream> #include<cstring> using namespace std; void mul(long long a[3][3],long long b[3][3]) { long long c[3][3]; memset(c,0,sizeof(c)); for (int i=0;i<3;i++) for (int j=0;j<3;j++) for (int k=0;k<3;k++) c[i][j] = (c[i][j]+a[i][k]*b[k][j]) % 1000000007; memcpy(a,c,sizeof(c)); } int main() { int T,x; cin>>T; for (int i=1;i<=T;i++) { long long a[3][3]={{0,0,1},{1,0,0},{0,1,1}}; long long f[3][3]={{1,1,1},{0,0,0},{0,0,0}}; cin>>x; x-=1; //因为第一个数存在了,所以计算次数-1 while (x>0) { if (x&1) mul(f,a); mul(a,a); x>>=1; } cout<<f[0][0]<<endl; } return 0; }