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. 就谈个py 的装饰器 decorator

    很早很早就知道有这么个 装饰器的东西,叫的非常神秘. 包括c#  和 java 中都有这个东西, c#中叫做attribut 特性,java中叫做Annotation 注解,在偷偷学习c#教程的时候, ...

  2. Microsoft Visual Studio Ultimate 2013 旗舰版 有效注册密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  3. 使用OpenXml把Excel中的数据导出到DataSet中

    public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...

  4. http中的KeepAlive

    为什么要使用KeepAlive? 终极的原因就是需要加快客户端和服务端的访问请求速度.KeepAlive就是浏览器和服务端之间保持长连接,这个连接是可以复用的.当客户端发送一次请求,收到相应以后,第二 ...

  5. PAT乙级真题1005. 继续(3n+1)猜想 (25)(解题)

    原题: 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证 ...

  6. ExtJs 4.2 treePanel 点击树节点 传送参数到后台(多个参数)

    //***********************************************左边树开始********************************************** ...

  7. JSP页面用EL表达式 输出date格式

    JSP页面用EL表达式 输出date格式 1.头上引入标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix ...

  8. python参考手册--第4、5、6、7章

    1.zip zip(s,t):将序列组合为一个元组序列[(s[0],t[0]), (s[1],t[1]), (s[2],t[2]), (s[3],t[3]),...] >>> s = ...

  9. SSH架构简单总结

    Struts.spring.Hibernate在各层的作用 1)struts 负责 web层.    ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forw ...

  10. 5.查找最小的k个元素(数组)

    题目: 输入n个整数,输出其中最小的k个,例如输入1,2,3,4,5,6,7,8这8个数,则最小的4个是1,2,3,4(输出不要求有序) 解: 利用快速排序的partition,算导上求第k大数的思想 ...