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;
}

  

转载于:https://www.cnblogs.com/bk-201/p/7489636.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值