Blocks POJ - 3734 (矩陣快速冪)
阿新 • • 發佈:2018-10-31
題意:
題解:
附上程式碼:
#include<iostream> #include<cstdio> using namespace std; typedef long long ll; const ll mod=1e4+7; const int N=4; struct node{ ll a[N][N]; }; node shu,ans,mp; node matrix(node x,node y) { for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ mp.a[i][j]=0; for(int p=1;p<=N;p++){ mp.a[i][j]=(mp.a[i][j]+x.a[i][p]*y.a[p][j]+mod)%mod; } } } return mp; } void work(ll k) { for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ ans.a[i][j]=0; } } for(int i=1;i<=N;i++){ ans.a[i][i]=1; } node t=shu; while(k){ if(k&1){ ans=matrix(ans,t); } k>>=1; t=matrix(t,t); } } int main() { shu.a[1][1]=2;shu.a[1][2]=1;shu.a[1][3]=0; shu.a[2][1]=2;shu.a[2][2]=2;shu.a[2][3]=2; shu.a[3][1]=0;shu.a[3][2]=1;shu.a[3][3]=2; int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); work(n); printf("%d\n",ans.a[1][1]); } return 0; return 0; }