poj3734 Blocks[矩阵优化dp or 组合数学]

Blocks
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6578   Accepted: 3171

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

Source

方法1:
//f[i]=6*f[i-1]-8*f[i-2]{i>=3,f[1]=2,f[2]=6}
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;
const ll mod=;
struct matrix{
ll s[][];
matrix(){
memset(s,,sizeof s);
}
}A,F;int n,T;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
c.s[i][j]+=a.s[i][k]*b.s[k][j];
c.s[i][j]%=mod;
}
}
}
return c;
}
matrix fpow(matrix a,int p){
matrix res;
for(int i=;i<;i++) res.s[i][i]=;
for(;p;p>>=,a=a*a) if(p&) res=res*a;
return res;
}
int main(){
for(scanf("%d",&T);T--;){
scanf("%d",&n);
if(n==){puts("");continue;}
if(n==){puts("");continue;}
A.s[][]=;A.s[][]=-;
A.s[][]=;A.s[][]=;
F.s[][]=;F.s[][]=;
F.s[][]=;F.s[][]=;
A=fpow(A,n-);
F=A*F;
printf("%lld\n",(F.s[][]+mod)%mod);
}
return ;
}

方法2:

//f(n)=2^(2n-2)+2^(n-1)
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;
const ll mod=;
ll ans=;int T,n;
ll fpow(ll a,ll p){
ll res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
int main(){
for(scanf("%d",&T);T--;){
scanf("%d",&n);
ans=fpow(,n-<<)+fpow(,n-);
printf("%I64d\n",(ans+mod)%mod);
}
return ;
}
上一篇:poj3734 Blocks


下一篇:Atomikos 中文说明文档【转】