[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 ...
随机推荐
- wpf鼠标捕获与控件交互——UIElement.CaptureMouse
应用场景是这样的,我需要拖动一个元素在屏幕上移动,注册了被移动元素的MouseMove事件,但是当鼠标移到被移动元素的外面时,移动失效,且鼠标的手势变成了普通的箭头形状,于是就找到了以下的解决方案. ...
- fedroa 更换字体
1. 添加RPM包: rpm -Uvh http://www.infinality.net/fedora/linux/infinality-repo-1.0-1.noarch.rpm 2. 安装包 y ...
- fedora 禁止nouveau加载
To remove / disable nouveau drivers from kernel initramfs ## Backup old initramfs nouveau image ## m ...
- IOS设备滑动事件
只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent: 当用户触摸到屏幕时调用方法 – t ...
- POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
- UUID为36位
package util; import java.util.UUID; public class UUIDUtil { public static UUID getId(){ return UUID ...
- 1064: [Noi2008]假面舞会 - BZOJ
Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办 ...
- 3.5 spring-replaced-method 子元素的使用与解析
1.replaced-method 子元素 方法替换: 可以在运行时用新的方法替换现有的方法,与之前的 look-up不同的是replace-method 不但可以动态地替换返回的实体bean,而且可 ...
- hdu 4726
贪心 不是很难 各种细节注意 #include <cstdio> #include <cstring> #include <algorithm> using na ...
- Java 方法覆盖和方法重载
方法重载(overloaded),要求方法的名称相同,参数列表不相同. 方法覆盖(override),要求①方法名相同,②参数列表相同,③返回值相同 如果是方法覆盖,要注意以下几种情况: 1.子类方法 ...