[POJ 3420] Quad Tiling

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=0)
    {
        size=s;
        memset(a,0,sizeof(a));
    }
    Matric operator * (const Matric &t)
    {
        Matric res=Matric(size);
        for(int i=0;i<size;i++)
        {
            for(int k=0;k<size;k++)
            {
                if((*this).a[i][k])
                    for(int j=0;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=0;i<size;i++) ans.a[i][i]=1;
        while(n)
        {
            if(n&1) ans=ans*(*this);
            (*this)=(*this)*(*this);
            n>>=1;
        }
        return ans;
    }
    void debug()
    {
        for(int i=0;i<size;i++)
        {
            for(int j=0;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(4);
        Matric b=Matric(4);
        a.a[0][0]=1;
        a.a[0][1]=1;
        a.a[0][2]=5;
        a.a[0][3]=11;

        b.a[0][3]=-1;
        b.a[2][3]=5;
        b.a[1][0]=b.a[2][1]=b.a[3][2]=b.a[1][3]=b.a[3][3]=1;

        b=b^n;
        a=a*b;
        printf("%d\n",(a.a[0][0]+MOD)%MOD);
    }
    return 0;
}
时间: 2024-08-02 00:04:06

[POJ 3420] Quad Tiling的相关文章

POJ 3420 Quad Tiling 题解 《挑战程序设计竞赛》

POJ 3420 Quad Tiling贴瓷砖:4*N的地板上用2*1的瓷砖铺满,求所有方案数对M求余.3.4熟练掌握动态规划矩阵的幂久违地上了节课,太无聊,只好刷一题.假设S[n]表示填满n时的方案数,有S[0]=1.定义矩阵M[p][q] := 边缘p和边缘q可以拼合时取1,否则取0所谓的可以拼合表示,两个边缘拼起来后长度为1(为2就拼接不起来了),且内部缝隙可以用2*1的瓷砖填满.那么M就有一些简单的性质了,比如M的第一行应该是:0 0 0 0 0 0... 继续阅读:码农场 » POJ

poj 3420 Quad Tiling 状压dp

题意: 给4*n(n<10^9)的大矩形,问有多少种用1*2的小矩形填满的方案. 分析: 又是铺瓷砖,不过这次n太大,不能再一个一个格的dp了.可以先算出相邻两行的状态转移,再用矩阵来加速n行的状态转移. 代码: //poj 3420 //sep9 #include <iostream> using namespace std; const int maxN=16; struct MATRIX { __int64 m[maxN][maxN]; }mat; int n,mod; void

POJ 3420 Quad Tiling 状压DP+矩阵快速幂

链接:http://poj.org/problem?id=3420 题意:给一个4*N(1 ≤ N ≤ 1e9)的矩形空间,并且给不限块数的1*2的多米诺骨牌,问是由多少种方式能把这个矩形空间填满. 思路:看到这种问题果断想到状压,虽然是在看矩阵的时候看到的这道题.dp[i][j]表示在第i行状态为j的情况下的填满方式数,j的二进制表示中0表示对应位置上一行的骨牌是竖放,或者对应位置的骨牌是横放,1则表示该行该位置的骨牌是竖放.由于N最大1e9所以O(n)的DP绝对超时,用矩阵快速幂来加速DP递

POJ3420 Quad Tiling (矩阵加速状压dp)

传送门:http://poj.org/problem?id=3420 Quad Tiling Time Limit: 1000MS   Memory Limit: 65536K       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 ≤ 1

POJ3420 Quad Tiling【矩阵快速幂】

Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5008 Accepted: 2269 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)

POJ 2506:Tiling

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7452   Accepted: 3639 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

POJ3420 Quad Tiling DP + 矩阵快速幂

题目大意是用1*2的骨牌堆积成4*N的矩形,一共有多少种方法,N不超过10^9. 这题和曾经在庞果网上做过的一道木块砌墙几乎一样.因为骨牌我们可以横着放,竖着放,我们假设以4为列,N为行这样去看,并且在骨牌覆盖的位置上置1,所以一共最多有16种状态.我们在第M行放骨牌的时候,第M+1行的状态也是有可能被改变的,设S(i,j)表示某一行状态为i时,将其铺满后下一行状态为j的方案书.考虑下如果我们让矩阵S和S相乘会有什么意义,考虑一下会发现S*S的意义当某行状态为i,接着其后面第2行的状态为j的可行

POJ 2663 Tri Tiling

Tri Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 5237 Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sample tiling of a 3x12 rectangle.  Input Input consists of several

poj 2663 Tri Tiling 状压dp

题意: 给3*N(N<=30)的矩形,问有多少种用1*2的小矩形铺满的方案. 分析: 同poj2411. 代码: #include <iostream> using namespace std; __int64 ans[32][4]; int n,m; __int64 dp[2][1<<4]; __int64 solve() { int i,j,used; memset(dp,0,sizeof(dp)); __int64 *crt=dp[0],*nxt=dp[1]; crt[