向前走莫回头❤

【POJ 3734】Blocks(递推+矩阵快速幂)

Blocks
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6293 Accepted: 3022

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

[题意][有n个格子,可以涂上红、绿、蓝或黄色,规定:红色格子和绿色格子的个数只能是偶数个,问方案数]
【题解】【递推+矩阵快速幂】
【其实是道水题,刚开始的递推式为:f[i][0][0]表示第i位红色和绿色都是偶数的方案数;f[i][0][1]表示第i位绿色是奇数,红色是偶数的方案数;f[i][1][0]表示第i位红色是奇数,绿色是偶数的方案数;f[i][1][1]表示第i位红色和绿色都是奇数的方案数】
f[i][x][y]=

 f[i][0][0]=f[i1][0][1]+f[i1][1][0]+f[i1][0][0]2f[i][0][1]=f[i1][1][1]+f[i1][0][0]+f[i1][0][1]2f[i][1][0]=f[i1][1][1]+f[i1][0][0]+f[i1][1][0]2f[i][1][1]=f[i1][0][1]+f[i1][1][0]+f[i1][1][1]2

【然后,会发现,109级别的数据根本存不开。。。考虑矩阵优化】
我们根据每次转移时,每一项所乘的系数构造转移矩阵:

2110120110210112

而初始矩阵为:
[f[1][0][0]f[1][0][1]f[1][1][0]f[1][1][1]]

=>

[2110]

朴素递推程序:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 10007
using namespace std;
int f[100000010][2][2],n[110],T,m;
int main()
{
    freopen("int.txt","r",stdin);
    freopen("my.txt","w",stdout);
    int i,j;
    scanf("%d",&T);
    for(i=1;i<=T;++i) scanf("%d",&n[i]),m=max(n[i],m);
    f[1][1][0]=1; f[1][0][1]=1; f[1][0][0]=2; f[1][1][1]=0;
    for(int i=2;i<=m;++i)
     {
        f[i][1][1]=f[i-1][0][1]+f[i-1][1][0]+f[i-1][1][1]*2,f[i][1][1]%=mod;
        f[i][1][0]=f[i-1][1][1]+f[i-1][0][0]+f[i-1][1][0]*2,f[i][1][0]%=mod;
        f[i][0][1]=f[i-1][1][1]+f[i-1][0][0]+f[i-1][0][1]*2,f[i][0][1]%=mod;
        f[i][0][0]=f[i-1][0][1]+f[i-1][1][0]+f[i-1][0][0]*2,f[i][0][0]%=mod;
     }
    for(int i=1;i<=T;++i) printf("%d\n",f[n[i]][0][0]%mod);
    return 0;
}

矩阵优化程序:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 10007
using namespace std;
struct node{
    int d[5][5];
}bank;
int T,A[5];
inline node jc(node a,node b)
{
    node c;
    memset(c.d,0,sizeof(c.d));
    for(int i=1;i<=4;++i)
     for(int j=1;j<=4;++j)
      for(int l=1;l<=4;++l)
       c.d[i][j]=(c.d[i][j]+a.d[i][l]*b.d[l][j]%mod)%mod;
    return c;
}
node poww(node a,int p)
{
    if(p==1) return bank;
    if(p%2)
      {
        node x=poww(a,p/2);
        x=jc(x,x);
        x=jc(x,a);
        return x;
      }
     else
      {
        node x=poww(a,p/2);
        x=jc(x,x);
        return x;
      }
}
inline int solve(int x)
{
    if(!x) return A[1];
    node c=poww(bank,x);
    int as[5];
    memset(as,0,sizeof(as));
    for(int i=1;i<=4;++i)
     for(int j=1;j<=4;++j)
      as[j]=(as[j]+A[i]*c.d[i][j]%mod)%mod;
    return as[1];
}
int main()
{
    //freopen("int.txt","r",stdin);
    //freopen("my.txt","w",stdout);
    int i,j;
    A[1]=2; A[2]=A[3]=1; A[4]=0;
    bank.d[1][1]=bank.d[2][2]=bank.d[3][3]=bank.d[4][4]=2;
    bank.d[1][4]=bank.d[2][3]=bank.d[3][2]=bank.d[4][1]=0;
    bank.d[1][2]=bank.d[1][3]=bank.d[2][1]=bank.d[2][4]=bank.d[3][1]=bank.d[3][4]=bank.d[4][2]=bank.d[4][3]=1; 
    scanf("%d",&T);
    while(T--)
     {
        int x;
        scanf("%d",&x);
        int ans=solve(x-1);
        printf("%d\n",ans);
     }
    return 0;
}
posted @ 2016-10-26 17:44  lris0-0  阅读(105)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m