您的位置:首页 > 其它

51Nod-1032-骨牌覆盖 V2

2017-11-11 01:41 204 查看
ACM模版

描述



题解

数据弱化的一个题,原题是 51Nod1033骨牌覆盖V2,插头 DP。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int MAXN = 1 << 5;

ll dp[MAXN][MAXN];
ll ret[MAXN][MAXN];
ll tmp[MAXN][MAXN];

int m, n = 3;

void dfs(int col, int pre, int now)
{
if (col > n)
{
return ;
}
if (col == n)
{
dp[pre][now]++;
return ;
}

dfs(col + 1, pre << 1, (now << 1) | 1);
dfs(col + 1, (pre << 1) | 1, now << 1);
dfs(col + 2, pre << 2 , now << 2);
}

void mul(ll ret[][MAXN], ll a[][MAXN], ll b[][MAXN])
{
int t = 1 << n;
for (int i = 0; i < t; i++)
{
for (int j = 0; j < t; j++)
{
ll tmp = 0;
for (int k = 0; k < t; k++)
{
tmp += a[i][k] * b[k][j];
tmp %= MOD;
}
ret[i][j] = tmp;
}
}
}

int main()
{
scanf("%d", &m);

dfs(0, 0, 0);

int t = 1 << n;
for (int i = 0; i < t; i++)
{
ret[i][i] = 1;
}

m++;
while (m)
{
for (int i = 0; i < t; i++)
{
for (int j = 0; j < t; j++)
{
tmp[i][j] = ret[i][j];
}
}

if (m & 1)
{
mul(ret, tmp, dp);
}
m = m >> 1;
mul(tmp, dp, dp);
for (int i = 0; i < t; i++)
{
for (int j = 0; j < t; j++)
{
dp[i][j] = tmp[i][j];
}
}
}

cout << ret[0][t - 1] << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  插头-dp