您的位置:首页 > 其它

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 company
in 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)可以用经典的矩阵快速幂来求得
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: