已过,但这种写法问题出在哪了?
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL mod=1e9+7;
struct M{
LL a[5][5];
}x,y;
M mul(M m,M n){
M ret;
memset(ret.a,0,sizeof(ret.a));
for(int i = 1;i<=3;i++){
for(int j = 1;j<=3;j++){
for(int k = 1;k<=3;k++){
ret.a[i][j]+=(m.a[i][k]%mod)*(n.a[k][j]%mod);
ret.a[i][j]%=mod;
}
}
}
return ret;
}
M qpow(M yy,LL b){
M ans;
memset(ans.a,0,sizeof(ans.a));
for(int i = 1;i<=3;i++) ans.a[i][i]=1;
while(b){
if(b&1) ans=mul(ans,yy);
yy=mul(yy,yy);
b>>=1;
}
return ans;
}
int main(){
LL t; scanf("%lld",&t);
x.a[1][1]=1; x.a[1][2]=0; x.a[1][3]=1;
x.a[2][1]=1; x.a[2][2]=0; x.a[2][3]=0;
x.a[3][1]=0; x.a[3][2]=1; x.a[3][3]=0;
while(t--){
LL n;
for(int i = 1;i<=3;i++)
for(int j = 1;j<=3;j++)
y.a[i][j]=x.a[i][j];
scanf("%lld",&n);
//错误
M tmp=qpow(y,n-3);
printf("%lld\n",tmp.a[1][1]+tmp.a[1][2]+tmp.a[1][3]);
//正确
M tmp=qpow(y,n);
printf("%lld\n",tmp.a[2][1]);
}
}