POJ3420Quad Tiling(矩阵快速幂)
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]|
相乘
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <algorithm>
#define LL long long
using 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;
}
POJ3420Quad Tiling(矩阵快速幂)的更多相关文章
- POJ 2663 Tri Tiling 矩阵快速幂 难度:3
Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7841 Accepted: 4113 Descri ...
- ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 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 ...
- 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 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
随机推荐
- TFS任务更新
由于不熟悉TFS任务更新的操作,花了四五个小时一个个的新建任务.下图是部分更新的任务截图: 每个任务的估计时间为3~5小时,每位成员的任务总时长均为19~20小时. 项目完成需要的总时间为135小时.
- 在html中如何获取表单提交的数据
a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- zju(11)在IAR中移植ucos到msp430
准备材料 1.在TI官网上下载430的固件库,我用的是msp430f5528的板子,下载的是F5xx_F6xx_Core_Lib 地址http://www.ti.com/tool/msp-exp430 ...
- iOS 编程思想
一 面向过程编程: 处理事情以过程为核心,一步一步的实现 二 面向对象编程: 万物皆对象 三 链式编程思想: 将多个操作通过点链接在一起成为一句代码 特点:方法返回值是Block,block必须有一个 ...
- Android课程---单选框与复选框的实现
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- what's the difference between dim as and dim as new?
what's the difference between dim as and dim as new? There is no difference with value types (Intege ...
- SQL Server 定时自动备份数据库
在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库,这篇文章介绍使用SQL Server 数据库代理中的作业定时自动备份数据库. 1.启动SQL Server代理服务,如下图: 绿色 ...
- Git commit 常见用法
Git commit git commit 主要是将用户通过git add命令添加到暂存区里的改动给提交到本地的版本库,关于版本库的构成可以查看我先前的笔记. 每次提交我们都会在本地版本库生成 ...
- ubuntu /etc/network/interfaces 中配置虚拟链路
ubuntu /etc/network/interfaces 中配置虚拟链路 平常做一些关于网络的测试时,像一些需要在二层上运行的功能,一个网卡不足够的情况下,可使用 ip link 工具加一些虚拟的 ...
- linux下安装编译php的curl扩展
curl扩展的位置(需要编译的版本)/root/install/php-5.5.24/ext/curl 1.进入对应的扩展目录 # cd /root/install/php-5.5.24/ext/cu ...