题目链接:http://poj.org/problem?id=3734
Blocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3922 Accepted: 1744
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
解题报告
dp TLE的时候就要考虑矩阵快速幂。
方法:
我们设 DP[n][4];
DP[i][0] 表示有i个blocks,其中红色和绿色都是偶数;
DP[i][1] 表示有i个blocks,其中红色是偶数绿色是奇数
DP[i][1] 表示有i个blocks,其中红色是奇数绿色是偶数
DP[i][1] 表示有i个blocks,其中红色是奇数绿色是奇数
dp[i][0]=2*dp[i-1][0](黄色黑色随便放一个有两种选择)+dp[i-1]1+dp[i-1]2
同理可以推出其他几种情况
但是由于n为10^9 普通DP会超时 因此我们只能构造矩阵
有了DP公式写出状态转移矩阵就恨轻松了
matrix A={//状态转移矩阵
2,1,1,0,
1,2,0,1,
1,0,2,1,
0,1,1,2
};
然后运用矩阵的快速幂解决问题。
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
#define mod 10007;
//dpTLE的时候往往就是要用矩阵快速幂
struct matrix
{
int m[4][4];
}; //一个矩阵
matrix d={{{2,1,1,0},{1,2,0,1},{1,0,2,1},{0,1,1,2}}}; //状态转移矩阵
//矩阵的乘法
matrix mul(matrix a,matrix b)
{
matrix res;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
res.m[i][j]=0;
for(int k=0;k<4;k++)
{
res.m[i][j]=res.m[i][j]+a.m[i][k]*b.m[k][j];
res.m[i][j]%=mod;
}
}
}
return res;
}
//矩阵快速幂
matrix pow(matrix a,int b) //a为底,b为次方
{
matrix base=a;
matrix ans={{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}};
while (b)
{
if(b & 1)
ans =mul(ans,base);
base=mul(base,base);
b=b>>1;
}
return ans; //ans就是快速幂的结果
}
int main(int argc, char** argv) {
int t,n;
cin >> t;
while(t--)
{
scanf("%d",&n);
matrix ans=pow(d,n);
printf("%d\n",ans.m[0][0]);
}
return 0;
}
https://blog.csdn.net/bigbigship/article/details/28280639
本文详细解析了POJ 3734 Blocks问题,通过矩阵快速幂解决大规模数据集下的组合计数问题。利用状态转移矩阵进行高效计算,避免了传统动态规划方法的超时问题。

2361

被折叠的 条评论
为什么被折叠?



