HDU 6185 Covering 矩阵快速幂加速dp

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6185

题意:用1x2的方块填满4xn的棋盘有多少种方法

 

先用dfs跑出合法状态

然后用矩阵快速幂优化

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
struct p
{
    ll a[16][16];
    p(){memset(a,0,sizeof(a));}
    p operator *(const p&t)
    {
        p ans;
        for(int i=0;i<16;i++)
            for(int k=0;k<16;k++)
                if (a[i][k])
                for(int j=0;j<16;j++)
                    ans.a[i][j]=(ans.a[i][j]+a[i][k]*t.a[k][j])%mod;
        return ans;
    }
};
p tem;
ll n;
void dfs(int l,int pre,int now)
{
    if (l>4) return;
    if (l==4)
    {
        tem.a[pre][now]++;
        return;
    }
    dfs(l+1,pre<<1|1,now<<1);
    dfs(l+1,pre<<1,now<<1|1);
    dfs(l+2,pre<<2|3,now<<2|3);
}
p qpow(p x,ll m)
{
    p ans;
    for(int i=0;i<16;i++)
        ans.a[i][i]=1;
    while(m)
    {
        if (m&1) ans=ans*x;
        x=x*x;
        m>>=1;
    }
    return ans;
}
int main()
{
    dfs(0,0,0);
    while(scanf("%lld",&n)!=EOF)
    {
        p ans=qpow(tem,n);
        printf("%lld\n",ans.a[15][15]);
    }
    return 0;
}

  

posted @ 2017-09-07 14:47  BK_201  阅读(122)  评论(0编辑  收藏  举报