Quad Tiling

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 3740 Accepted: 1684

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

递推式:a[i]=a[i-1]+5*a[i-2]+a[i-3]-a[i-4];

由于N高达10^9,所以要用矩阵进行优化。

|0 1 0 0|

|0 0 1 0|

|0 0 0 1|

|-1 1 5 1|



|a[i-3]|

|a[i-2]|

|a[i-1]|

|a[i]|

相乘

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <queue>
  6. #include <cstdlib>
  7. #include <algorithm>
  8. #define LL long long
  9. using namespace std;
  10. const int Max = 10;
  11. int Mod;
  12. struct Matrix
  13. {
  14. int n,m;
  15. int a[Max][Max];
  16. void clear()//清空矩阵
  17. {
  18. n=0;
  19. m=0;
  20. memset(a,0,sizeof(a));
  21. }
  22. Matrix operator * (const Matrix &b)const//矩阵相乘
  23. {
  24. Matrix tmp;
  25. tmp.clear();
  26. tmp.n=n;
  27. tmp.m=b.m;
  28. for(int i=0;i<n;i++)
  29. {
  30. for(int j=0;j<b.m;j++)
  31. {
  32. for(int k=0;k<m;k++)
  33. {
  34. tmp.a[i][j]=(tmp.a[i][j]+(a[i][k]%Mod)*(b.a[k][j]%Mod))%Mod;
  35. }
  36. }
  37. }
  38. return tmp;
  39. }
  40. };
  41. void Pow(int m)
  42. {
  43. Matrix s;
  44. s.clear();
  45. s.n=4;
  46. s.m=4;
  47. s.a[3][3]=1;s.a[3][2]=5;
  48. s.a[3][1]=1;s.a[3][0]=-1;
  49. s.a[1][2]=1;s.a[2][3]=1;
  50. s.a[0][1]=1;
  51. Matrix ans;
  52. ans.clear();
  53. ans.n=4;
  54. ans.m=1;
  55. ans.a[0][0]=1;
  56. ans.a[1][0]=5;
  57. ans.a[2][0]=11;
  58. ans.a[3][0]=36;
  59. while(m)//快速幂
  60. {
  61. if(m&1)
  62. {
  63. ans=s*ans;
  64. }
  65. s=s*s;
  66. m>>=1;
  67. }
  68. printf("%d\n",ans.a[3][0]);
  69. }
  70. int main()
  71. {
  72. int n;
  73. while(scanf("%d %d",&n,&Mod),n)
  74. {
  75. if(n<4)
  76. {
  77. switch(n)
  78. {
  79. case 1:
  80. printf("%d\n",1%Mod);
  81. break;
  82. case 2:
  83. printf("%d\n",5%Mod);
  84. break;
  85. case 3:
  86. printf("%d\n",11%Mod);
  87. break;
  88. }
  89. continue;
  90. }
  91. Pow(n-4);
  92. }
  93. return 0;
  94. }

POJ3420Quad Tiling(矩阵快速幂)的更多相关文章

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

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

  2. ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

    Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  5. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  8. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  10. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

随机推荐

  1. 搞懂Path环境变量

    path:环境变量 我们平时打开一个应用程序,一般是双击桌面图标或在开始菜单链接,无论是桌面的快捷图标还是菜单链接都包含了应用程序的安装位置信息,打开它们的时候系统会按照这些位置信息找到安装目录然后启 ...

  2. CI框架不能有Index控制器

    今天部署了ci框架,想用用它.创建别的控制器没什么错误.但是我创建了一个Index控制器,并访问了index方法,报错了.但是直接在方法中写输出就没事.而且方法名称改为其他部位index的也能访问. ...

  3. 理解listagg函数

    两道SQL面试题引出listagg函数: 1. 用一条sql求出每个部门(emp表)的最大工资和最小工资,以及最大工资和最小工资的员工姓名. (注:一次表扫描.同一个部门最大工资或最小工资的人可能不止 ...

  4. C++复制对象时勿忘每一部分

    现看这样一个程序: void logCall(const string& funcname) //标记记录 { cout <<funcname <<endl; } cl ...

  5. 使用 JavaScript 实现基本队列、优先队列和循环队列

    1.基本队列的实现 基本队列的方法中,包含了 ①向队列(尾部)中添加元素(enqueue).②(从队列头部)删除元素(dequeue).③查看队列头部的元素(front).④查看队列是否为空(isEm ...

  6. C#中ref和out的使用与区别

    C#中ref关键字和out关键字所实现的功能差不多,都是指定一个形参按照引用传递而不是实参的副本传递.但是二者适用场景还是有些区别的:out适合用在需要retrun多个返回值的地方,而ref则适合用在 ...

  7. innodb_buffer_pool_size 大小建议

    innodb_buffer_pool_size参数大小建议: 查看Innodb_buffer_pool_pages_data大小,即已使用页面 MySQL> SHOW GLOBAL STATUS ...

  8. Object[]arr代码输出奇怪字符的解释

    代码:class  lizi  { public static void main(String[] args){ //TODO  Auto-generated method stub Object[ ...

  9. py操作mysql

    1.操作mysql的标准流程 import pymysql conn = pymysql.connect(host = "127.0.0.1", port = 3306,user ...

  10. ls 只显示目录

    只显示目录: ls -d */ 在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ls 命令的 -d 选项: $ ls -d */ 2. 利用 ls 命令的 -F 选项: ...