[POJ 3420] Quad Tiling
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3495 | Accepted: 1539 |
Description
Tired of the Tri Tiling game finally, Michael turns to a more challengeable game, Quad Tiling:
In how many ways can you tile a 4 × N (1 ≤ N ≤ 109) rectangle with 2 × 1 dominoes? For the answer would be very big, output the answer modulo M (0 < M ≤ 105).
Input
Input consists of several test cases followed by a line containing double 0. Each test case consists of two integers, N and M, respectively.
Output
For each test case, output the answer modules M.
Sample Input
1 10000
3 10000
5 10000
0 0
Sample Output
1
11
95
Source
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 10 int MOD;
struct Matric
{
int size;
int a[N][N];
Matric(int s=)
{
size=s;
memset(a,,sizeof(a));
}
Matric operator * (const Matric &t)
{
Matric res=Matric(size);
for(int i=;i<size;i++)
{
for(int k=;k<size;k++)
{
if((*this).a[i][k])
for(int j=;j<size;j++)
{
res.a[i][j]+=(ll)(*this).a[i][k]*t.a[k][j]%MOD;
res.a[i][j]=(res.a[i][j]+MOD)%MOD;
}
}
}
return res;
}
Matric operator ^ (int n)
{
Matric ans=Matric(size);
for(int i=;i<size;i++) ans.a[i][i]=;
while(n)
{
if(n&) ans=ans*(*this);
(*this)=(*this)*(*this);
n>>=;
}
return ans;
}
void debug()
{
for(int i=;i<size;i++)
{
for(int j=;j<size;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
};
int main()
{
int n;
while(scanf("%d%d",&n,&MOD),n||MOD)
{
Matric a=Matric();
Matric b=Matric();
a.a[][]=;
a.a[][]=;
a.a[][]=;
a.a[][]=; b.a[][]=-;
b.a[][]=;
b.a[][]=b.a[][]=b.a[][]=b.a[][]=b.a[][]=; b=b^n;
a=a*b;
printf("%d\n",(a.a[][]+MOD)%MOD);
}
return ;
}
[POJ 3420] Quad Tiling的更多相关文章
- POJ 3420 Quad Tiling (矩阵乘法)
[题目链接] http://poj.org/problem?id=3420 [题目大意] 给出一个4*n的矩阵,求用1*2的骨牌填满有多少方案数 [题解] 弄出不同情况的继承关系,用矩阵递推即可. [ ...
- poj 3420 Quad Tiling (状压dp+多米诺骨牌问题+矩阵快速幂)
还有这种操作?????? 直接用pre到now转移的方式构造一个矩阵就好了. 二进制长度为m,就构造一个长度为1 << m的矩阵 最后输出ans[(1 << m) - 1][( ...
- POJ 2663 Tri Tiling 【状压DP】
Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sample tilin ...
- 【poj3420】 Quad Tiling
http://poj.org/problem?id=3420 (题目链接) 题意 给出$n*m$的网格,用$1*2$的方块覆盖有多少种方案. Solution 数据很大,不能直接搞了,我们矩乘一下.0 ...
- POJ 2663 Tri Tiling
Tri Tiling Time Li ...
- POJ 2663 Tri Tiling 矩阵快速幂 难度:3
Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7841 Accepted: 4113 Descri ...
- poj3420 Quad Tiling
传送门 题目大意 问讲一个大小为4*n的棋盘用无数1*2的骨牌不重叠覆盖有多少种方案. 分析 我们考虑可以将长为n的棋盘分为两块,一个大小为n-i,另一个大小为i,而为了避免对于不同的i构造出相同的情 ...
- POJ3420 Quad Tiling DP + 矩阵高速幂
题目大意是用1*2的骨牌堆积成4*N的矩形.一共同拥有多少种方法,N不超过10^9. 这题和以前在庞果网上做过的一道木块砌墙差点儿一样. 由于骨牌我们能够横着放.竖着放.我们如果以4为列,N为行这样去 ...
- poj 2663 Tri Tiling (状压dp+多米诺骨牌问题+滚动数组反思)
本来直接一波状压dpAC的 #include<cstdio> #include<cstring> #include<algorithm> #define REP(i ...
随机推荐
- 使用commons-fileUpload组件上传文件
在近期的一个项目中有用到commons-fileUpload组件进行实现文件上传的功能(由于没用到框架),在使用的过程中有遇到一些问题,经过自己的琢磨也算顺利地将其解决了,在这里做个记录. 一.com ...
- 转:深入学习Oracle分区表及分区索引
转自:http://database.ctocio.com.cn/tips/286/8104286.shtml 关于分区表和分区索引(About Partitioned Tables and Inde ...
- EXTJS 4.2 资料 控件之Grid 添加行,编辑行,删除行
//SiteVariableConfigValue类,创立一个模型类 Ext.define("SiteVariableConfigValue", { extend: "E ...
- win7 提升windows服务权限使非管理员用户可以控制windows服务的开启和关闭
#include <windows.h>#include <tchar.h>#include <strsafe.h>#include <aclapi.h> ...
- 1185: [HNOI2007]最小矩形覆盖 - BZOJ
就是一道凸包(枚举凸包的边作为矩形的一条边)的裸题,只是不太好打,所以犹豫到今天才打 不说了,说起AC都是泪啊,因为没有精度判断,没有判重(算距离时除0了)错了好久 拍了好久都和标称是一样的,因为 ...
- Spring MVC与表单日期提交的问题
Spring MVC与表单日期提交的问题 spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常. org.springframework.beans.BeanIn ...
- replace()替换文字
var test = text.innerHTML; b = test.replace(/任晓强/g,"你好"); console.log(b); html: <div id ...
- [转载]MongoDB优化的几点原则
.查询优化 确认你的查询是否充分利用到了索引,用explain命令查看一下查询执行的情况,添加必要的索引,避免扫表操作. .搞清你的热数据大小 可能你的数据集非常大,但是这并不那么重要,重要的是你的热 ...
- 搭建 Android 开发环境,初试HelloWorld (win7) (上) (转)
搭建Android开发环境主要有以下几步要做: 1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD 1.JDK(Java Development K ...
- ural 1221
本来就是个很水的题 就是枚举起点长度然后直接判断就行了 但是比赛的时候写了个大bug 还找不出来 自己太水了 #include <cstdio> #include <c ...