题目大意:
给定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; }
原文地址:https://www.cnblogs.com/visualVK/p/9119331.html
时间: 2024-10-31 03:55:08