Nice Patterns Strike Back

Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

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

Source

Andrew Stankevich Contest 1
 
 
算法:因为m<=5,每一行的状态可以用一个二进制数表示,构造系数矩阵A,其中aij为1表示状态i和j不冲突,为0表示冲突,结果为A^(n-1)中矩阵各元素之和,由于n很大,所以涉及到高精度和矩阵快速幂,所以我用Java写了。
 
 
 
 import java.awt.Checkbox;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner; public class Main { static int p; public static class Matrix implements Cloneable {
long[][] a;
int d; public Matrix(int d) {
this.d = d;
a = new long[d][d];
} public Matrix multiply(Matrix m) {
Matrix ret = new Matrix(d);
for (int i = 0; i < d; ++i) {
for (int j = 0; j < d; ++j) {
for (int k = 0; k < d; ++k) {
ret.a[i][j] += a[i][k] * m.a[k][j];
ret.a[i][j] %= p;
}
}
}
return ret;
} public Matrix clone() {
Matrix ret = new Matrix(d);
ret.a = a.clone();
return ret;
} Matrix pow(BigInteger cnt) {
// 先生成一个单位矩阵
Matrix eye = new Matrix(d);
for (int i = 0; i < d; i++)
eye.a[i][i] = 1; for (int i = cnt.bitLength() - 1; i >= 0; i--) {
eye = eye.multiply(eye);
if (cnt.testBit(i)) {
eye = eye.multiply(this);
}
}
return eye;
}
} static boolean check(int x, int y, int m) {
for (int i = 1; i < m; i++) {
if ((x & 3) == (y & 3) && (x & 1) == ((x & 2) >> 1)) {
return false;
}
x >>= 1;
y >>= 1;
} return true;
} public static void main(String[] args) { Scanner cin = new Scanner(new BufferedInputStream(System.in));
PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out)); int T = cin.nextInt(); while (T-- != 0) {
BigInteger n = cin.nextBigInteger();
int m = cin.nextInt();
p = cin.nextInt(); // 生成矩阵A
int d = (1 << m);
Matrix A = new Matrix(d);
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++) {
if (check(i, j, m))
A.a[i][j] = 1;
} A = A.pow(n.subtract(BigInteger.ONE)); long ans = 0;
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++) {
ans = (ans + A.a[i][j]) % p;
} cout.println(ans);
if (T != 0)
cout.println("");
// System.out.println(ans);
} cin.close();
cout.close(); }
}

ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度的更多相关文章

  1. HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  3. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  4. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  5. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  6. 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 ...

  7. 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 输 ...

  8. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  9. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

随机推荐

  1. ios 限制输入长度

    ----------------UITextField限制输入的长度------------ - (BOOL)textField:(UITextField *)textField shouldChan ...

  2. Neighbour table overflow --- arp表溢出

    [root@jiangyi01.sqa.zmf /home/ahao.mah] #grep . /proc/sys/net/ipv4/neigh/default/gc_thresh* /proc/sy ...

  3. AngularJS和ReactJS对比

    Angular的特点: 优势: AngularJS是一套完整的框架,angular有自带的数据绑定.render渲染.angularUI库,过滤器,$filter,$directive(模板),$se ...

  4. 二、MLlib统计指标之关联/抽样/汇总

    汇总统计[Summary statistics]: Summary statistics提供了基于列的统计信息,包括6个统计量:均值.方差.非零统计量个数.总数.最小值.最大值. import org ...

  5. Jsp中response对象的所有属性

    所属接口:javax.servlet.http.HttpServletResponse,其父接口是ServletResponse,而且ServletResponse也现在只有唯一一个HttpServl ...

  6. VS2008编程软件过期的问题,过期弹出须要升级窗体的解决的方法

    找到安装文件,再点autorun.exe安装文件,然后反复安装过程就会弹出须要填写系列号的地方,天上以下第一个系列号就可以. Visual Studio 2008 Professional Editi ...

  7. X-FORWARDED-FOR

    外界流传的JAVA/PHP服务器端获取客户端IP都是这么取的: 伪代码: 1)ip = request.getHeader("X-FORWARDED-FOR")     可伪造,参 ...

  8. Python3.2官方文档翻译--继承

    6.5 继承 当然,一门语言特性假设不支持继承那么名称类就失去了价值.子类继承父类的方法例如以下: class DerivedClassName(BaseClassName): <stateme ...

  9. VS2010调试小技巧

    在VS下做开发的时候我们进行调试的时候路径是这个样子的:http://localhost:端口号/项目名称/index.aspx 但是发布到服务器上面的时候却是这个样子的:http://www.xxx ...

  10. HID 报告描述符精细说明.

    1,报告描述符概述    1.1) 报表描述符        报表描述符和USB的其他描述符是不一样的,它不是一个简单的表格,报表描述符是USB所有描述符中最复杂的.报表描述符非常复杂而有弹性,因为它 ...