Quad Tiling

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 4107

Accepted: 1878

Description

Tired of(厌烦) the Tri Tiling gamefinally, Michael turns to(转向) a morechallengeable game, Quad Tiling:

In how many ways can you tile(铺瓷砖) a 4 × N (1 ≤ N ≤109) rectangle(矩形) with 2 × 1dominoes(多米诺骨牌)? For the answerwould be very big, output the answer modulo M (0 < M ≤105).

Input

Input consists of several test casesfollowed by a line containing double 0. Each test case consists of twointegers, N and M, respectively(分别的).

Output

For each test case, output the answermodules M.

Sample Input

1 10000

3 10000

5 10000

0 0

Sample Output

1

11

95

Source

POJMonthly--2007.10.06, Dagger

题意:在一个4*N的矩阵中用2*1的多米诺骨牌铺满,问有多少种铺法?

模板:

递推式: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]| 
相乘

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <algorithm>
#define LL long longusing namespace std;
const int  Max = 10;int Mod;
struct Matrix
{int n,m;int a[Max][Max];void clear()//清空矩阵{n=0;m=0;memset(a,0,sizeof(a));}Matrix operator * (const Matrix &b)const//矩阵相乘{Matrix tmp;tmp.clear();tmp.n=n;tmp.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++){tmp.a[i][j]=(tmp.a[i][j]+(a[i][k]%Mod)*(b.a[k][j]%Mod))%Mod;}}}return tmp;}
};void Pow(int m)
{Matrix s;s.clear();s.n=4;s.m=4;s.a[3][3]=1;s.a[3][2]=5;s.a[3][1]=1;s.a[3][0]=-1;s.a[1][2]=1;s.a[2][3]=1;s.a[0][1]=1;Matrix ans;ans.clear();ans.n=4;ans.m=1;ans.a[0][0]=1;ans.a[1][0]=5;ans.a[2][0]=11;ans.a[3][0]=36;while(m)//快速幂{if(m&1){ans=s*ans;}s=s*s;m>>=1;}printf("%d\n",ans.a[3][0]);
}int main()
{ int n;while(scanf("%d %d",&n,&Mod),n){if(n<4){switch(n){case 1:printf("%d\n",1%Mod);break;case 2:printf("%d\n",5%Mod);break;case 3:printf("%d\n",11%Mod);break;}continue;}Pow(n-4);}return 0;
}

POJ3420 Quad Tiling(模板+矩阵快速幂)相关推荐

  1. POJ3420 Quad Tiling【矩阵快速幂】

    Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5008 Accepted: 2269 Descripti ...

  2. POJ3070 矩阵快速幂模板

    题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...

  3. 43行代码AC_HDU-2604 Queuing(矩阵快速幂,附详细的知识讲解、模板例题)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 传送门3.1-->HDU-1575(经典矩阵快速幂模板题1) 传送门3.2--& ...

  4. 矩阵快速幂及斐波那契数列模板

    本篇博客先给出矩阵快速幂以及利用矩阵快速幂求斐波那契数列的模板,讲解待更新-- const int N=10; int tmp[N][N]; void multi(int a[][N],int b[] ...

  5. 算法分类整理+模板①:矩阵快速幂

    一直有一个想法,感觉自己很多基础算法不是很扎实,想要找个机会写一些算法的整理,顺便自己总结一些实用的模板. 最近偶然在训练赛中连续做了2道思维+矩阵快速幂的题目,碰巧有时间,就以矩阵快速幂作为这个系列 ...

  6. AtCoder abc256全题解(区间合并模板、矩阵快速幂优化dp、线段树……)

    文章目录 A B C-枚举 D-区间合并模板 E-图论建模,函数图的性质 题意 思路 代码 F-树状数组 题意 思路 代码 G-矩阵快速幂优化dp H-线段树 思路 实现 传送门 本文CSDN 本文j ...

  7. 【洛谷P3390】 矩阵快速幂(模板)

    贴一下矩阵快速幂的模板 #include<iostream> #include<cstdio> #include<cstring> #include<stri ...

  8. 快速幂+矩阵快速幂模板

    快速..运算 快速幂 运用位运算 代码 分析 矩阵快速幂 题目 分析 代码 拓一..: 快速幂 运用位运算 强大的位运算把我搞得蒙蒙的 理解了之后我表示很喜欢!!! 代码 int power(int ...

  9. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

最新文章

  1. 使用JMX监控Kafka
  2. Linux从入门到精通系列之SHELL编程循环语句语法及实例详解(forwhileuntil)
  3. local path of sap-ui-core-less-140903345-dbg.js
  4. Linux_linux常用工具(git,vim ,gcc ,gdb,权限)超详解
  5. centos使用git安装nvm
  6. javascript中Array的操作
  7. CentOS7.1下targetcli的使用
  8. elasticsearch中的优先级线程池
  9. android p preview_细数 Android P 开发者预览版中最不能错过的新特性
  10. 洛谷——P2613 【模板】有理数取余
  11. ES6的类Class基础知识点
  12. php圆角的度数计算公式,弧度和角度的换算器(度数换算计算器)
  13. 如何才能更好发挥WinRunner,实现真正的自动化测试
  14. wpa_supplicant2.9编译过程
  15. 新人服务器上快速简单搭建cs
  16. iOS App上架遇到的错误(ERRORITMS-90096: )
  17. VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录。VMX进程已经提前退出。
  18. 一个步骤结束机房电脑红蜘蛛的控制
  19. linux 'stack'未声明(在此函数内第一次使用,未定义的引用`__stack_chk_fail'
  20. Python数据库篇

热门文章

  1. 现代软件工程 作业 3 团队作业
  2. Meeters and Greeters 接客大厅
  3. Nginx的开启和关闭
  4. python通讯录运用的知识点_案例驱动式Python学习--通讯录存取
  5. 读取24位ad的值_实践案例丨利用小熊派开发板获取土壤湿度传感器的ADC值
  6. iptables 指定网卡_LINUX系统下的IPTABLES防火墙系统讲解(二)实战操作
  7. html封装windows,windows 系统封装,打造一份属于自己的系统!
  8. 基于单片机超声波测距系统的设计_一种基于UWB技术实现的测距防撞系统
  9. html如何将设置文本效果,css如何对文本进行修饰
  10. mysql 自身参照自身_MySQL入门