题目描述
a[1]=a[2]=a[3]=1
a[x]=a[x-3]+a[x-1] (x>3)
求a数列的第n项对1000000007(10^9+7)取余的值。
输入输出格式
输入格式:
第一行一个整数T,表示询问个数。
以下T行,每行一个正整数n。
输出格式:
每行输出一个非负整数表示答案。
输入输出样例
输入样例#1:
3 6 8 10
输出样例#1:
4 9 19
说明
对于30%的数据 n<=100;
对于60%的数据 n<=2*10^7;
对于100%的数据 T<=100,n<=2*10^9;
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<algorithm> #include<math.h> #include<cstring> using namespace std; #define LL unsigned long long #define MOD 1000000007 struct node{ LL v[3][3]; }x,b,ans,p; LL T,n; node cheng(node x,node y) { for(int i=0;i<=2;i++) for(int j=0;j<=2;j++) { p.v[i][j]=0; for(int k=0;k<=2;k++) p.v[i][j]=(p.v[i][j]+x.v[i][k]*y.v[k][j])%MOD; } return p; } void fastlow() { while(n) { if(n%2) ans=cheng(ans,x); x=cheng(x,x);n/=2; } } int main() { scanf("%lld",&T); b.v[0][1]=b.v[0][2]=b.v[0][0]=1; x.v[0][0]=x.v[0][1]=1; x.v[1][2]=1; x.v[2][0]=1; ans=cheng(b,x); for(int i=1;i<=T;i++) { scanf("%lld",&n); x.v[0][0]=x.v[0][1]=1;x.v[0][2]=0; x.v[1][2]=1;x.v[1][0]=x.v[1][1]=0; x.v[2][0]=1;x.v[2][1]=x.v[2][2]=0; ans=cheng(b,x); if(n<=3) { printf("1 "); continue; } n-=4;fastlow(); printf("%lld ",ans.v[0][0]); } return 0; }
要注意的问题:
因为有T组数据,所以要初始化“乘数单元“(0,1都要初始化!)。
记得要调用函数名,只写括号是不对但能编译过的。这个错误最难找。