ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
Nice Patterns Strike Back
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
Output
Sample Input
2 2 5
3 3 23
Sample Output
4
0
Source
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:矩阵快速幂,高精度的更多相关文章
- HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 矩阵快速幂 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 ...
随机推荐
- 浅谈zygote服务中的设计思路
zygote服务是Android启动和服务APK的核心服务,每个APK都是通过zygote启动,今日阅读它的源码学习到一个不错的设计思路. 首先看看一个APK通过zygote的启动流程: 按照一般的设 ...
- java笔记3之赋值运算符
赋值运算符: 基本的赋值运算符:= 把=右边的数据赋值给左边. 扩展的赋值运算符:+=,-=,*=,/=,%= += 把左边 ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- [ES6] Promise
How to use: export default function getReplies(topicId){ return new Promise(function( resolve, rejec ...
- iOS动画开发之五——炫酷的粒子效果
在上几篇博客中,我们对UIView层的动画以及iOS的核心动画做了介绍,基本已经可以满足iOS应用项目中所有的动画需求,如果你觉得那些都还不够炫酷,亦或是你灵光一现,想用UIKit框架写出一款炫酷的休 ...
- 关于CCRect
一直有一个误区,因为之前处理的公司引擎是屏幕坐标系 导致觉得CCRect的坐标起始值(x,y),习惯性的认为就是左上角的点. 但是,真正的x,y值,是跟x轴与y轴相对应的.
- html 使用表单标签,与用户交互
使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务器端程序就可以处理表单传过来的数据. 语法: <form ...
- ASP.NET MVC上传文件的几种方法
1.Form表单提交 <p>Form提交</p> <form action="@Url.Action("SavePictureByForm" ...
- 邮件发送 EMailHelper
引用: using System; using System.Collections.Generic; using System.Linq; using System.Net; using Syste ...
- displaytag 简单使用流程
1. 首先导入包:displaytag-1.2.jar,commons-lang.jar和standard.jar;commons-beanutils.jar;这四个jar包 2. 然后在jsp页面做 ...