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

POJ Monthly--2007.10.06, Dagger
 
川大校赛的原题出处,,,醉了,,比赛的时候一直没推出公式,唉,弱得不行
重点在求递推公式,再矩阵快速幂即可。
SCU 4430 把输入改一下就可以了
#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的更多相关文章

  1. POJ 3420 Quad Tiling (矩阵乘法)

    [题目链接] http://poj.org/problem?id=3420 [题目大意] 给出一个4*n的矩阵,求用1*2的骨牌填满有多少方案数 [题解] 弄出不同情况的继承关系,用矩阵递推即可. [ ...

  2. poj 3420 Quad Tiling (状压dp+多米诺骨牌问题+矩阵快速幂)

    还有这种操作?????? 直接用pre到now转移的方式构造一个矩阵就好了. 二进制长度为m,就构造一个长度为1 << m的矩阵 最后输出ans[(1 << m) - 1][( ...

  3. POJ 2663 Tri Tiling 【状压DP】

    Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes?  Here is a sample tilin ...

  4. 【poj3420】 Quad Tiling

    http://poj.org/problem?id=3420 (题目链接) 题意 给出$n*m$的网格,用$1*2$的方块覆盖有多少种方案. Solution 数据很大,不能直接搞了,我们矩乘一下.0 ...

  5. POJ 2663 Tri Tiling

                                                                                    Tri Tiling   Time Li ...

  6. POJ 2663 Tri Tiling 矩阵快速幂 难度:3

    Tri Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7841   Accepted: 4113 Descri ...

  7. poj3420 Quad Tiling

    传送门 题目大意 问讲一个大小为4*n的棋盘用无数1*2的骨牌不重叠覆盖有多少种方案. 分析 我们考虑可以将长为n的棋盘分为两块,一个大小为n-i,另一个大小为i,而为了避免对于不同的i构造出相同的情 ...

  8. POJ3420 Quad Tiling DP + 矩阵高速幂

    题目大意是用1*2的骨牌堆积成4*N的矩形.一共同拥有多少种方法,N不超过10^9. 这题和以前在庞果网上做过的一道木块砌墙差点儿一样. 由于骨牌我们能够横着放.竖着放.我们如果以4为列,N为行这样去 ...

  9. poj 2663 Tri Tiling (状压dp+多米诺骨牌问题+滚动数组反思)

    本来直接一波状压dpAC的 #include<cstdio> #include<cstring> #include<algorithm> #define REP(i ...

随机推荐

  1. Delphi代码优化

    文章编目 1. 字符串优化 1.1. 不重复初始化 1.2. 使用SetLength预分配长字符串(AnsiString) 1.3. 字符串与动态数组的线程安全(Thread Safety) 1.4. ...

  2. Django的templates模版

    前面介绍的都是django.http.HttpResponse 把内容直接返回到网页上,这次介绍通过渲染模版的方法来显示内容 步骤: 1.创建一个项目(略) 2.创建一个app(略) 3.添加项目到s ...

  3. 1079. Total Sales of Supply Chain (25)

    时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  4. 【MongoDB】开启认证权限

    1. mongodb.conf : 添加 auth=true 2. use admin (3.0+ 使用 createUser ;<3.0版本  http://www.cnblogs.com/g ...

  5. Timeline

    Timeline面板   Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. 更新时间:201 ...

  6. hadoop Safe mode is ON 的解决办法

    hadoop Safe mode is ON 的解决办法 搭了一个hadoop集群环境,近期总是出现读写文件错误的情况,查看name node的日志显示 (Safe mode is ON) Safe ...

  7. NOI考前乱写

    还有13天NOI,把各种乱七八糟的算法都重新过一遍还是比较有必要的... //HDU 5046 Airport //DancingLink #include<iostream> #incl ...

  8. 在js中获取easyui datagrid的数据

    可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法,但是怎么用了,没说. 最后这样写才搞定 var a ...

  9. Java 另一道构造器与构造器重载的题目

    题目: 请写出以下程序的输出结果 public class ConstructorTest2 { public static void main(String[] args) { new B(&quo ...

  10. SSH hibernate 使用时最好添加访问数据库的编码

    SSH hibernate 使用时最好添加访问数据库的编码 如下所示:第13行为设置hibernate访问数据库的编码(&是&的转义序列) <!DOCTYPE hibernate ...