Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4268 | Accepted: 1918 |
Description
Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.
Input
The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.
Output
For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.
Sample Input
2 1 2
Sample Output
2 6
题意:求n个格子涂色后,使得红色和绿色的数量是偶数的组合方式有多少种。
思路:an表示n个格子涂色后满足要求的情况有多少种,bn表示n个格子涂色后红色和绿色有一种是奇数有多少种,cn表示n个格子涂色后红色和绿色均是奇数有多少种。
a(n)=a(n-1)*2+b(n-1);b(n)=a(n-1)*2+b(n-1)*2+c(n-1)*2;c(n)=b(n-1)+c(n-1)*2;
所以矩阵构造为
a1 b1 c1 2 2 0
0 0 0 * ( 1 2 1 )^(n-1)
0 0 0 0 2 2
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int f[4][4];
};
node first,ret,a,b;
int MOD=10007;
node mul(node A,node B)
{
node C;
int i,j,k;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
{
C.f[i][j]=0;
for(k=1;k<=3;k++)
C.f[i][j]=(C.f[i][j]+A.f[i][k]*B.f[k][j])%MOD;
}
return C;
}
int main()
{
int T,t,n,i,j,k,p;
scanf("%d",&T);
a.f[1][1]=2;a.f[1][2]=2;a.f[1][3]=0;
b.f[1][1]=2;b.f[1][2]=2;b.f[1][3]=0;
b.f[2][1]=1;b.f[2][2]=2;b.f[2][3]=1;
b.f[3][1]=0;b.f[3][2]=2;b.f[3][3]=2;
for(t=1;t<=T;t++)
{
scanf("%d",&n);
if(n==1)
printf("2\n");
else
{
p=n-2;
ret=b;first=b;
while(p)
{
//printf("%d\n",p);
if(p&1)
ret=mul(ret,first);
first=mul(first,first);
p/=2;
}
ret=mul(a,ret);
printf("%d\n",ret.f[1][1]);
}
}
}