zoj 2317 Nice Patterns Strike Back(矩阵乘法)
problemId=1317">http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=1317
给出一个n*m的矩阵(n <= 10^100, m <= 5),对于2*2的子方格若全是黑色或全是白色的是非法的,用黑白两色去染n*m的方格,问共同拥有多少种合法的染色方案。
构造出转移矩阵,上一行向下一行的转移矩阵。由于m<=5,每行最多有32个状态。能够进行状态压缩构造出一个32*32的转移矩阵A。A[i][j] = 1表示上一行i状态能够向下一行的j状态转移。否则不能转移。要求最后的合法方案数,就再构造一个B矩阵,是一个32*1的矩阵,表示了到达第一行每个状态的方案数。那么A*B就表示到达第二行每个状态的方案数。以此类推。A^n-1 * B表示到达第n行每个状态的合法方案数,那么全部状态相应方案数的和就是总的方案数。
由于n特别大。要用到大数。我存矩阵的时候開始定义的大数类,一直T。改成了int型才A,1s+,难道大数类这么慢么,5s都过不了。
import java.io.*;
import java.math.BigInteger;
import java.util.Scanner; class Matrix
{
public int [][] mat = new int[35][35];
public Matrix()
{ }
public void init()
{
for(int i = 0; i < 35; i++)
{
for(int j = 0; j < 35; j++)
{
if(i == j)
mat[i][j] = 1;
else mat[i][j] = 0;
}
}
}
} public class Main {
public static Matrix A,B,res;
public static BigInteger n;
public static int m,p,test,mm;
static Scanner cin = new Scanner(System.in); public static void buildA()
{
for(int i = 0; i < mm; i++)
{
for(int j = 0; j < mm; j++)
{
String s1 = Integer.toBinaryString(i);
String s2 = Integer.toBinaryString(j);
String ss1 = new String();
String ss2 = new String();
for(int k = 0; k < m-s1.length(); k++)
ss1 += '0';
ss1 += s1;
for(int k = 0; k < m-s2.length(); k++)
ss2 += '0';
ss2 += s2; int flag = 0;
for(int k = 0; k < m-1; k++)
{
if(ss1.charAt(k)-'0' == 0 && ss1.charAt(k+1)-'0' == 0
&& ss2.charAt(k)-'0' == 0 && ss2.charAt(k+1)-'0' == 0)
{flag = 1;break;}
if(ss1.charAt(k)-'0' == 1 && ss1.charAt(k+1)-'0' == 1
&& ss2.charAt(k)-'0' == 1 && ss2.charAt(k+1)-'0' == 1)
{flag = 1;break;}
}
if(flag == 1)
A.mat[i][j] = 0;
else A.mat[i][j] = 1;
}
}
} public static Matrix Mul(Matrix x,Matrix y)
{
Matrix ans = new Matrix();
for(int i = 0; i < mm; i++)
{
for(int k = 0; k < mm; k++)
{
if(x.mat[i][k] == 0)
continue;
for(int j = 0; j < mm; j++)
{
ans.mat[i][j] = (ans.mat[i][j]+x.mat[i][k]*y.mat[k][j])%p;
}
}
}
return ans;
} public static Matrix Pow(Matrix tmp,BigInteger nn)
{
Matrix ans = new Matrix();
ans.init();
while(nn.compareTo(BigInteger.ZERO) > 0)
{
if( nn.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.ONE) == 0)
ans = Mul(ans,tmp);
nn = nn.divide(BigInteger.valueOf(2));
tmp = Mul(tmp,tmp);
}
return ans;
} public static void main(String[] args) throws IOException
{
int test = cin.nextInt();
while((test--) > 0)
{
n = cin.nextBigInteger();
m = cin.nextInt();
p = cin.nextInt();
mm = (1<<m); A = new Matrix();
B = new Matrix(); buildA();
for(int i = 0; i < mm; i++)
B.mat[i][0] = 1; res = Pow(A,n.subtract(BigInteger.ONE));
res = Mul(res,B); int anw = 0;
for(int i = 0; i < mm; i++)
{
anw = (anw+res.mat[i][0])%p;
}
System.out.println(anw);
if(test > 0)
System.out.println();
}
}
}
zoj 2317 Nice Patterns Strike Back(矩阵乘法)的更多相关文章
- ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)
We consider problems concerning the number of ways in which a number can be written as a sum. If the ...
- ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...
- ZOJ 2671 Cryptography 矩阵乘法+线段树
B - Cryptography Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 3256 Tour in the Castle 插头DP 矩阵乘法
题解 这题是一道非常好的插头题,与一般的按格转移的题目不同,由于m很大,要矩阵乘法,这题需要你做一个按列转移的插头DP. 按列转移多少与按格转移不同,但大体上还是基于连通性进行转移.每一列只有右插头是 ...
- *HDU2254 矩阵乘法
奥运 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...
- *HDU 1757 矩阵乘法
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- bzoj 2738 矩阵乘法
其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...
随机推荐
- MySQ学习笔记之十 NULL值处理
这是MySQL一大特殊之处. 概念上.NULL意味着"没有值"或"未知值",且它被看作有点与众不同的值. 为了測试NULL.你不能使用算术比較运算符比如=.&l ...
- 手机验证 发送验证码倒计时js
html: <input name="Tel" class="weui-input" type="tel" placeholder=& ...
- Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- 用fcntl锁一个文件来保护操作
int testfd; /* fd for test*/ if((testfd = open("/usr/local/pgsql/bin/test_fd",O_RDWR|O_CRE ...
- Oracle的Clob转换类型
import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; imp ...
- 使用 validate 进行输入验证
validate 官方教程网址: http://www.runoob.com/jquery/jquery-plugin-validate.html 在表单页面引入两个核心 js 文件 #官方的两个文件 ...
- 第一章、zabbix安装
前言: 注意:本文不涉及性能测试.性能优化中的监控,所有文字的出发点都是日常运维监控. 在开始之前,我们还是先统一下认识:要监控一个对象,需要掌握哪些东西呢? 监控对象的理解:要监控的对象你是否了解呢 ...
- unity C# StackOverflowException
有时候图省事,属性这样写public int pageCount{get{return pageCount;}set{pageCount=value;}}可能会报栈溢出的错误, StackOverfl ...
- ArcGIS api for javascript——鼠标悬停时显示信息窗口
描述 本例展示当用户在要素上悬停鼠标时如何显示InfoWindow.本例中,要素是查询USA州图层的QueryTask的查询结果.工作流程如下: 1.用户单击一个要素 2.要素是“加亮的”图形. 3. ...
- Get,Post和Head具体解释
HTTP请求最经常使用的三个方法: (1)Get方法. 取回请求URL标志的不论什么信息,在浏览器的地址栏中输入网址的方式訪问网页时,浏览器採用GET方法向server获取资源. (2)Post方法. ...