隐藏页面特效

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=10007; struct matrix{ ll s[2][2]; matrix(){ memset(s,0,sizeof s); } }A,F;int n,T; matrix operator *(const matrix &a,const matrix &b){ matrix c; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ for(int k=0;k<2;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=0;i<2;i++) res.s[i][i]=1; for(;p;p>>=1,a=a*a) if(p&1) res=res*a; return res; } int main(){ for(scanf("%d",&T);T--;){ scanf("%d",&n); if(n==1){puts("2");continue;} if(n==2){puts("6");continue;} A.s[0][0]=6;A.s[0][1]=-8; A.s[1][0]=1;A.s[1][1]=0; F.s[0][0]=6;F.s[0][1]=0; F.s[1][0]=2;F.s[1][1]=0; A=fpow(A,n-2); F=A*F; printf("%lld\n",(F.s[0][0]+mod)%mod); } return 0; }

方法2:

 

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

 

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6725996.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(297)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示