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) 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

问题链接:POJ3420 Quad Tiling
问题简述:(略)
问题分析
    递推式如下:
a[0]=1,a[1]=1,a[2]=5,a[3]=11,
a[n]=a[n-1]+5a[n-2]+a[n-3]-a[n-4](n >= 4)
    采用矩阵快速模幂计算,模板题。关键是找到那个递推式。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ3420 Quad Tiling */#include<cstdio>
#include<cstring>using namespace std;const int N = 10;
const int M = 10;int r, mod;struct Matrix
{int n,m;int a[N][M];void clear(){n = m = 0;memset(a, 0, sizeof(a));}Matrix operator +(const Matrix &b) const{Matrix c;c.n = n;c.m = m;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)c.a[i][j] = a[i][j] + b.a[i][j];return c;}Matrix operator -(const Matrix &b) const{Matrix c;c.n = n;c.m = m;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)c.a[i][j] = a[i][j] - b.a[i][j];return c;}Matrix operator *(const Matrix &b) const{Matrix c;c.clear();c.n = n;c.m = b.m;for(int i = 0; i < n; i++)for(int j = 0; j < b.m; j++)for(int k = 0; k < m; k++){c.a[i][j] += a[i][k] * b.a[k][j];c.a[i][j] %= mod;}return c;}Matrix powermod(int x){Matrix c;c.clear();c.n = c.m = n;for(int i = 0; i < n; i++)c.a[i][i] = 1;if(x == 0)return c;else if(x == 1)return *this;Matrix d = powermod(x / 2);d = d * d;if(x % 2 )d = d * (*this);return d;}
};int main()
{while(scanf("%d%d", &r, &mod) == 2 && (r || mod)) {int a[] = {1, 1, 5, 11};if(r <= 3) {printf("%d\n", a[r] % mod);continue;}Matrix b;b.clear();b.n = b.m = 4;b.a[0][1] = b.a[1][2] = b.a[2][3] = 1;b.a[3][0] = -1;b.a[3][1] = 1;b.a[3][2] = 5;b.a[3][3] = 1;b = b.powermod(r - 3);Matrix x;x.clear();x.n = 4;x.m = 1;x.a[0][0] = 1;x.a[1][0] = 1;x.a[2][0] = 5;x.a[3][0] = 11;x = b * x;printf("%d\n",(x.a[3][0] + mod) % mod);}return 0;
}

POJ3420 Quad Tiling【矩阵快速幂】相关推荐

  1. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

  2. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

  3. 快速幂 + 矩阵快速幂

    快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...

  4. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  5. [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]

    解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...

  6. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

  7. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  8. HDU 6185 Covering 矩阵快速幂 递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...

  9. bzoj 1409 Password 矩阵快速幂+欧拉函数

    可以发现,该数组的mi就是斐波那契数列 所以要矩阵快速幂搞出第n位 但是斐波那契数列上涨的很快,这就需要欧拉定理了 p^phi(q)%q=1(gcd(p,q)==1) p是素数,所以可以用 然后需要5 ...

最新文章

  1. r语言datarame删除行_R语言缺失值的处理:线性回归模型插补
  2. 如何让 Hyper-V 和 VMware 虚拟机软件共存?
  3. maven的仓库、生命周期与插件
  4. CSS3边框图片、边框阴影、文本阴影
  5. 污水处理中php是什么药剂,污水处理中需要用到哪些药剂?
  6. Bootstrap模态出现在背景下
  7. jquery匹配不区分大小写_jQuery实现contains方法不区分大小写的方法教程
  8. 华为的人力资源体系的变革
  9. 台北三日自助游攻略(转载)
  10. Network Switching Software Platform Guide学习笔记
  11. 2016 Multi-University Training Contest 3 1010 Rower Bo
  12. mysql转换戳转换成小时_MySQL时间函数 | 时间戳和日期之间得转换
  13. MyEcplise_Maven搭建SSM框架
  14. QLineEdit setFocus失效问题
  15. 大连海事大学信息与通信工程系808考研上岸经验分享
  16. android充电信息代码,【代码】android 关机充电
  17. 作为站长,给火绒吹一次,论杀毒谁强?
  18. 利用ENVI实现landsat 5地表温度反演
  19. 电脑系统还原,电脑系统还原步骤,ghost系统还原教程
  20. 2021牛年一月营销活动指导方案

热门文章

  1. 树莓派7寸触屏,略贵
  2. centos mysql-5.5.20_mysql-5.5.20+CentOS 6.2 编译安装全过程详解(2)
  3. JavaWeb——MyBatis入门程序
  4. ArcGIS Maritime 发布海图切片服务详解
  5. [知乎] 端游、手游服务端架构演变
  6. Android App应用包增量升级(one)
  7. Shadow Map在DirectX9.0 SDK Sample 的实现方法
  8. 游戏中汉字显示的实现与技巧
  9. 浏览器 html 看层级,浏览器视图层级中的“根”:html和body的属性研究
  10. matlab状态方程 传递函数 可控性,matlab 控制系统仿真