题目大意是用1*2的骨牌堆积成4*N的矩形。一共同拥有多少种方法,N不超过10^9。

这题和以前在庞果网上做过的一道木块砌墙差点儿一样。

由于骨牌我们能够横着放。竖着放。我们如果以4为列,N为行这样去看,而且在骨牌覆盖的位置上置1,所以一共最多有16种状态。我们在第M行放骨牌的时候,第M+1行的状态也是有可能被改变的,设S(i,j)表示某一行状态为i时,将其铺满后下一行状态为j的方案书。考虑下如果我们让矩阵S和S相乘会有什么意义。考虑一下会发现S*S的意义当某行状态为i。接着其后面第2行的状态为j的可行方案数,一般地,S^n则代表接下来第n行状态为j的方案数,这里N非常大,我们能够用高速幂对矩阵的幂进行加速。

对于S矩阵的最初状态我们能够穷尽搜索来求。

#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map> #include<iostream>
#include<cstdio>
using namespace std;
int State[16][16];
int Start[16][16];
int IMod; void InitState(int ori, int s, int p)
{
bool isfull = true;
for (int i = 0; i < 4; i++)
{
if (((s >> i) & 1) == 0)
{
//竖着放
InitState(ori, s | (1 << i), p | (1 << i));
//横着放
if (i < 3 && ((s >> (i + 1)) & 1) == 0)
{
int tp = s | (1 << i);
tp |= (1 << (i + 1));
InitState(ori ,tp, p);
}
isfull = false;
break;
}
}
if (isfull)
{
State[ori][p] += 1;
}
} void Product(int a[][16], int b[][16], int res[][16])
{
for (int i = 0; i < 16;i++)
{
for (int j = 0; j < 16; j++)
{
res[i][j] = 0;
for (int k = 0; k < 16; k++)
{
res[i][j] += (a[i][k] * b[k][j] % IMod);
res[i][j] %= IMod;
}
}
}
} void QProduct(int p[][16], int res[16][16], int n)
{
memset(res, 0, sizeof(int) * 16 * 16);
int tmp[2][16][16];
int tmpres[16][16];
memcpy(tmp[0], p, sizeof(int) * 16 * 16);
int i = 0;
for (int k = 0; k < 16; k++)
{
res[k][k] = 1;
}
while (n)
{ if (n & 1)
{
memcpy(tmpres, res, sizeof(int) * 16 * 16);
Product(tmpres, tmp[i & 1], res);
}
Product(tmp[i & 1], tmp[i & 1], tmp[(i + 1) & 1]);
i++;
n = n >> 1;
}
} int main()
{
#ifdef _DEBUG
freopen("d:\\in.txt", "r", stdin);
#endif
int n, m;
memset(State, 0, sizeof(State));
for (int i = 0; i < 16; i++)
{
InitState(i, i, 0);
} int finstates[16][16];
int res[16][16];
memset(Start, 0, sizeof(Start));
Start[0][0] = 1;
while (scanf("%d %d", &n, &m) != EOF)
{
if (n == 0 || m == 0)
{
break;
}
IMod = m;
QProduct(State, finstates, n);
Product(Start, finstates, res);
int ans = 0;
for (int i = 0; i < 16; i++)
{
ans += res[i][0];
ans %= IMod;
}
printf("%d\n", ans);
}
return 0;
}

POJ3420 Quad Tiling DP + 矩阵高速幂的更多相关文章

  1. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  2. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

  3. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  4. hdu 5411 CRB and Puzzle 矩阵高速幂

    链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...

  5. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  6. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

  7. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

  8. HDU2842-Chinese Rings(递推+矩阵高速幂)

    pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...

  9. HDU2276 - Kiki &amp; Little Kiki 2(矩阵高速幂)

    pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...

随机推荐

  1. 【转】数据仓库ODS、DW和DM概念区分

    今天看了一些专业的解释,还是对ODS.DW和DM认识不深刻,下班后花时间分别查了查它们的概念. ODS——操作性数据 DW——数据仓库 DM——数据集市 1.数据中心整体架构   数据中心整体架构 数 ...

  2. redis 内存管理与数据淘汰机制(转载)

    原文地址:http://www.jianshu.com/p/2f14bc570563?from=jiantop.com 最大内存设置 默认情况下,在32位OS中,Redis最大使用3GB的内存,在64 ...

  3. Untiy CurvedUI 的使用的bug修正

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51996538 作者:car ...

  4. Mysql 使用命令及 sql 语句示例

    Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句. 在此处有 sql 语句使用示例:在这里 此处插入 ...

  5. 00032_ArrayList集合的遍历

    1.通过集合遍历,得到集合中每个元素,这是集合中最常见的操作 2.集合的遍历与数组的遍历很像,都是通过索引的方式 public class ArrayListDemo02 { public stati ...

  6. BZOJ 3747: [POI2015]Kinoman 【线段树】

    Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...

  7. mvc框架简介

    1.mvc( mode  view controller)是什么?   m指业务代码,v指用户界面,c是控制器 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式 是一种 ...

  8. [暑假集训--数位dp]hdu3652 B-number

    A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...

  9. 【JQ同胞遍历】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. BZOJ 2973 入门OJ4798 石头游戏

    矩阵递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring ...