Blocks POJ - 3734(白書例題)
阿新 • • 發佈:2018-06-01
con max fclose pair vector fun ret double lose
題目大意:
給定N個方塊排成一列。先用紅、藍、綠、黃四種顏色塗方塊,問紅色方塊跟綠色方塊同為偶數的方案有多少個
分析:
設塗到第i個方塊時,紅綠都是偶數的方案數為ai,兩者中只有一者為偶數bi,兩者都是奇數的方案ci,可以得 到下列遞推: (先說a[i+1]的遞推): 1.到i都為偶數,i+1個都塗另外兩種顏色中的一個(所以就是a[i]×2) 2.到i只有一個為奇數,i+1塗成奇數的那個(所以時b[i]) 綜上所述:a[i+1]=b[i]+a[i]×2 同理b[i+1]=2×a[i]+2×b[i]+2×c[i] c[i+1]=b[i]+2×c[i] /* a[i] |2 1 0|^n a[0](1) b[i] = |2 2 2| b[0](0) c[i] |0 1 2| c[0](0) */
code:
#define debug #include<stdio.h> #include<math.h> #include<cmath> #include<queue> #include<stack> #include<string> #include<cstring> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<functional> #include<iomanip> #include<map> #include<set> #define pb push_back #define dbg(x) cout<<#x<<" = "<<(x)<<endl; #define lson l,m,rt<<1 #define cmm(x) cout<<"("<<(x)<<")"; #define rson m+1,r,rt<<1|1 using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll>PLL; typedef pair<int,ll>Pil; typedef pair<ll,int>Pli; const ll INF = 0x3f3f3f3f; const ll inf=0x7fffffff; const double eps=1e-8; const int maxn =1000000; const int N = 510; const ll mod=1e9+7; const ll MOD=10007; //------ //define typedef vector<int> vec; typedef vector<vec> mat; mat mul(mat &A,mat&B){ mat C(A.size(),vec(B[0].size())); for(int i=0;i<A.size();i++){ for(int k=0;k<B.size();k++){ for(int j=0;j<B[0].size();j++){ C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD; } } } return C; } mat pow(mat A,ll n){ mat B(A.size(),vec(A.size())); //單位陣 I for(int i=0;i<A.size();i++){ B[i][i]=1; } while(n>0){ if(n&1)B=mul(B,A); A=mul(A,A); n>>=1; } return B; } //solve void solve() { int T; cin>>T; while(T--){ int N; cin>>N; mat A(3,vec(3)); A[0][0]=2;A[0][1]=1;A[0][2]=0; A[1][0]=2;A[1][1]=2;A[1][2]=2; A[2][0]=0;A[2][1]=1;A[2][2]=2; A=pow(A,N); cout<<A[0][0]<<endl; } } int main() { ios_base::sync_with_stdio(false); #ifdef debug freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif cin.tie(0); cout.tie(0); solve(); /* #ifdef debug fclose(stdin); fclose(stdout); system("out.txt"); #endif */ return 0; }
Blocks POJ - 3734(白書例題)