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的可行方案数,一般地,S^n则代表接下来第n行状态为j的方案数,这里N非常大,我们能够用高速幂对矩阵的幂进行加速。
对于S矩阵的最初状态我们能够穷尽搜索来求。
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map> #include<iostream>
#include<cstdio>
using namespace std;
int State[16][16];
int Start[16][16];
int IMod; void InitState(int ori, int s, int p)
{
bool isfull = true;
for (int i = 0; i < 4; i++)
{
if (((s >> i) & 1) == 0)
{
//竖着放
InitState(ori, s | (1 << i), p | (1 << i));
//横着放
if (i < 3 && ((s >> (i + 1)) & 1) == 0)
{
int tp = s | (1 << i);
tp |= (1 << (i + 1));
InitState(ori ,tp, p);
}
isfull = false;
break;
}
}
if (isfull)
{
State[ori][p] += 1;
}
} void Product(int a[][16], int b[][16], int res[][16])
{
for (int i = 0; i < 16;i++)
{
for (int j = 0; j < 16; j++)
{
res[i][j] = 0;
for (int k = 0; k < 16; k++)
{
res[i][j] += (a[i][k] * b[k][j] % IMod);
res[i][j] %= IMod;
}
}
}
} void QProduct(int p[][16], int res[16][16], int n)
{
memset(res, 0, sizeof(int) * 16 * 16);
int tmp[2][16][16];
int tmpres[16][16];
memcpy(tmp[0], p, sizeof(int) * 16 * 16);
int i = 0;
for (int k = 0; k < 16; k++)
{
res[k][k] = 1;
}
while (n)
{ if (n & 1)
{
memcpy(tmpres, res, sizeof(int) * 16 * 16);
Product(tmpres, tmp[i & 1], res);
}
Product(tmp[i & 1], tmp[i & 1], tmp[(i + 1) & 1]);
i++;
n = n >> 1;
}
} int main()
{
#ifdef _DEBUG
freopen("d:\\in.txt", "r", stdin);
#endif
int n, m;
memset(State, 0, sizeof(State));
for (int i = 0; i < 16; i++)
{
InitState(i, i, 0);
} int finstates[16][16];
int res[16][16];
memset(Start, 0, sizeof(Start));
Start[0][0] = 1;
while (scanf("%d %d", &n, &m) != EOF)
{
if (n == 0 || m == 0)
{
break;
}
IMod = m;
QProduct(State, finstates, n);
Product(Start, finstates, res);
int ans = 0;
for (int i = 0; i < 16; i++)
{
ans += res[i][0];
ans %= IMod;
}
printf("%d\n", ans);
}
return 0;
}
POJ3420 Quad Tiling DP + 矩阵高速幂的更多相关文章
- HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5062 Accepted: 1370 Description YYF i ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- hdu 5411 CRB and Puzzle 矩阵高速幂
链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
- HDU2276 - Kiki & Little Kiki 2(矩阵高速幂)
pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...
随机推荐
- appIcon
原文地址:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconM ...
- (转)自定义UITabBar
push页面时,可调用hidesBottomBarWhenPushed进行隐藏. 第一步,我们需要一些图片: 各个选项的图标和tabbar的背景图片,最后还要一个透明的1x1像素的图片. 第二步,新建 ...
- EFCore CodeFirst模型迁移生成数据库备注(mysql)
重写Mysql下sql脚本生成器 using Framework.NetCore.Extensions; using Framework.NetCore.Models; using Microsoft ...
- python基础学习笔记——time模块
time模块 time翻译过来就是时间,有我们其实在之前编程的时候有用到过. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取 ...
- 【JavaScript 1—基础知识点】:宏观概述
导读:JavaScript是一门新的(也可以说是旧的或者半新语言),里面有很多的知识点都能和已有的知识产生共鸣.但是,虽然简单,相同点也有很多,也有不同点.我脑袋也不好使,所以对于我来说,还是有必要再 ...
- .netCore例子
.netCore例子 文章:https://github.com/dotnet-architecture/eShopOnContainers
- 新一代 javascript 模板引擎
artTemplate-3.0 新一代 javascript 模板引擎 <!DOCTYPE html> <html lang="en"> <head& ...
- element-ul InputNumber 空白
if(this.days == undefined){ this.$nextTick(function(){ this.days = 1; }); }
- poj2945 Find the Clones
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8490 Accepted: 3210 D ...
- LINUX支持哪些文件系统
我们在Linux中常用的文件系统主要有ext3.ext2及reiserfs :Windows和Dos常用的文件系统是fat系列(包括fat16及fat32等)和ntfs 文件系统:光盘文件系统是ISO ...