ASC(1)E(矩阵快速幂+简单DP)
2014-10-10 00:59
507 查看
Nice Patterns Strike Back
Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)SubmitStatisticNext
Problem
Problem Description
You might have noticed that there is the new fashion among rich people to have their yards tiled with black and white tiles, forming a pattern. The company Broken Tiles is well known as the best tiling companyin our region. It provides the widest choices of nice patterns to tile your yard with. The pattern is nice if there is no square of size 2 × 2, such that all tiles in it have the same color. So patterns on the figure 1 are nice, while patterns on the figure
2 are not.
The president of the company wonders whether the variety of nice patterns he can provide to the clients is large enough. Thus he asks you to find out the number of nice patterns that can be used to tile the
yard of size N × M . Now he is interested in the long term estimation, so he suggests N ≤ 10100. However, he does not
like big numbers, so he asks you to find the answer modulo P .
Input
The input file contains three integer numbers: N (1 ≤ N ≤ 10100), M (1 ≤ M ≤ 5) and P (1 ≤ P ≤10000).
Output
Write the number of nice patterns of size N × M modulo P to the output file.
Sample Input
2 2 5 3 3 23
Sample Output
4 0
题意:RT
思路:用dp[i]表示前i行所有方案数,那么可以从dp[i]转移到dp[i+1],这个只需要累加第i和第i+1行满足的情况即可
但是n很大,所以是不可能for一遍n来求的
不难发现,每次的转移都是一样的,即只需要判断相邻2行的满足情况即可
由于m<=5,可以用状态压缩来表示相邻两行格子的状态,1代表黑,0代表白,构造一个矩阵M[i][j]
如果M[i][j]=1代表上一行的状态i和当前行的状态j是满足的
所以有dp[i]=( M^(i-1) )*dp[1],其中dp[1]=(1<<m)代表所有的方案都满足
而M^(i-1)可以用经典的矩阵快速幂来求得
相关文章推荐
- HDU 2157 How many ways??(简单线性DP | | 矩阵快速幂)
- HDU 2157 How many ways??(简单线性DP | | 矩阵快速幂)
- xjoj146 快速刷屏 -- 简单dp 居然没想到dp打表
- POJ 3734 —— 矩阵快速幂 + DP
- bnu 34985 Elegant String(矩阵快速幂+dp推导公式)
- hdu2604 Queuing(矩阵快速幂 + DP)
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
- POJ 3744 Scout YYF I 矩阵快速幂加速dp
- Codeforces 514E Darth Vader and Tree DP + 矩阵快速幂
- hdu 2604 queuing dfa dp + 矩阵快速幂
- 【矩阵二维或三维dp】最大子矩阵,子矩阵快速求和(用到最大直方图)
- POJ 3744 Scout YYF I(矩阵快速幂 概率dp)
- 一个简单的矩阵乘法(快速幂)
- hdu 1575 Tr A(矩阵快速幂,简单)
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
- POJ3735 Training little cats DP,矩阵快速幂,稀疏矩阵优化
- hdu 4576(简单概率dp | 矩阵优化)
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
- poj 3744 Scout YYF I (矩阵快速幂+概率dp)
- poj 3070 矩阵快速幂简单题