题意:N个方块一列,4种颜色去涂,其中红色和绿色需要涂偶数个(包括0),问有多少种不同的涂法。
从左往右涂,对于第i个格子,有ai(红色绿色都是偶数个)的方案数,bi(都奇),ci(红色奇绿色偶),di(绿色奇红色偶)。
从而可以构造出矩阵:
2 0 1 1
0 2 1 1
1 1 2 0
1 1 0 2
快速幂即可。
AC代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<bitset>
using namespace std;
#define ll __int64
#define eps 1e-8
#define NMAX 101000
#define MOD 10007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef vector<int> vec;
typedef vector<vec> mat;
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
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()));
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;
}
int a[4][4] = {2,0,1,1,0,2,1,1,1,1,2,0,1,1,0,2};
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o4.txt","w",stdout);
#endif // GLQ
mat t(4,vec(4));
for(int i = 0; i < 4; i++)
t[i].assign(a[i],a[i]+4);
int n,cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
mat A(1,vec(4));
A[0][0] = 1;
mat ans = pow(t,n);
ans = mul(A,ans);
printf("%d\n",ans[0][0]);
}
return 0;
}