Blocks
http://poj.org/problem?id=3734
題解:矩陣快速冪
構(gòu)造初始矩陣:
{0,1,0,1} {0,0,0,0} {0,0,0,0} {0,0,0,0}構(gòu)造轉(zhuǎn)移矩陣:
{3,1,3,1} {1,3,1,3} {0,0,0,0} {0,0,0,0}參考文章:?矩陣乘積? ??矩陣快速冪? ?矩陣構(gòu)造方法? ?
/* *@Author: STZG *@Language: C++ */ //#include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int #define endl "\n" using namespace std; typedef long long ll; //typedef __int128 lll; const int N=10+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,p,l,r,u,v,x,y; int ans,cnt,flag,temp,sum; int s[N][N]; int b[N][N]; int a[N][N]; char str; struct node{}; void Matrix(ll a[N][N],ll b[N][N]){ll c[N][N];memset(c,0,sizeof(c));for(int i=0;i<4;i++){for(int j=0;j<4;j++){for(int k=0;k<4;k++){c[i][j]=(c[i][j]+a[i][k]*b[k][j])%10007;}}}for(int i=0;i<4;i++){for(int j=0;j<4;j++){a[i][j]=c[i][j];}} } ll power(int k){ll b[N][N]={{3,1,3,1},{1,3,1,3},{0,0,0,0},{0,0,0,0}};ll a[N][N]={{0,1,0,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}};while(k){if(k&1)Matrix(a,b);Matrix(b,b);k>>=1;}return (a[0][1]+a[0][3])%10007; } int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);scanf("%d",&t);while(t--){scanf("%d",&n);cout<<power(n-1)<<endl;}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結(jié)
- 上一篇: Another kind of Fibo
- 下一篇: Tiling_easy version