[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 ...
随机推荐
- linux设备驱动模型(kobject与kset)
Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...
- dell inspiorn 14vr 1616b ubuntu 无线网卡的问题
找到两个解决方法: 1 找 网卡驱动下载: 用命令 以下 from :http://zhidao.baidu.com/link?url=k6QNIdJlbRyZJSEW1cVUs_1p4Jv-73c8 ...
- wpf鼠标捕获与控件交互——UIElement.CaptureMouse
应用场景是这样的,我需要拖动一个元素在屏幕上移动,注册了被移动元素的MouseMove事件,但是当鼠标移到被移动元素的外面时,移动失效,且鼠标的手势变成了普通的箭头形状,于是就找到了以下的解决方案. ...
- 浅谈string
#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 using std::string;using ...
- spot 5、ALOS监督分类波段组成
spot 5监督分类RGB:412 ALOS分类波段RGB:432
- sysfs接口整理
SYS节点 目录结构: 1:sysfs相关知识点介绍(介绍sysfs的体系结构) 2:sys节点核心知识(使用sys节点核心的知识) 3:代码实例(创建sys节点的代码实例) 1:sysfs相关知识点 ...
- C#时间戳与时间互转
/// <summary> /// 时间戳转成时间类型 /// </summary> /// <param name="timeStamp">& ...
- Js setInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)
最近在做项目时用到了定时执行的js方法,setInterval与setTimeout时间长了不用有些生疏了,所以自己总结了一下,记下来,以便以后使用. Document自带的方法: 循环执行:var ...
- unity手游之聊天SDK集成与使用一
手游中都有聊天功能,比如公会,私聊,世界聊天,那么找一个好用,功能强大的SDK的可以节省很多精力,帮助我们提高开发速度与游戏质量. 写本篇博文是为了方便使用这个SDK做聊天模块的程序,避免许多坑,我在 ...
- spring4+hibernate3
环境说明:spring4.0+hibernate3 数据库:oracle 连接池:c3p0 项目结构: lib中的jar: 一.配置spring.xml 说明:这里采用的配置模式将hibernateT ...