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 ...
随机推荐
- Linux多线程实践(六)使用Posix条件变量解决生产者消费者问题
前面的一片文章我们已经讲过使用信号量解决生产者消费者问题.那么什么情况下我们须要引入条件变量呢? 这里借用 http://www.cnblogs.com/ngnetboy/p/3521547.htm ...
- 用LogParser分析Windows日志
用LogParser分析Windows日志 实战案例分享 假设你已具有上面的基础知识,那么以下为你准备了更加深入的应用操作视频(从安装到使用的全程记录): http://www.tudou.com/p ...
- pyspark.mllib.feature module
Feature Extraction Feature Extraction converts vague features in the raw data into concrete numbers ...
- es6 --- class 类的继承使用
传统的javascript中只有对象,没有类的概念.它是基于原型的面向对象语言.原型对象特点就是将自身的属性共享给新对象.这样的写法相对于其它传统面向对象语言来讲,很有一种独树一帜的感脚!非常容易让人 ...
- java9新特性-22-总结
1.在java 9 中看不到什么? 1.1 一个标准化和轻量级的JSON API 一个标准化和轻量级的JSON API被许多java开发人员所青睐.但是由于资金问题无法在Java 9中见到,但并不会削 ...
- [转]A reference to a higher version or incompatible assembly cannot be added to the project
If you face this error message while adding a reference to your project in Visual Studio try the fol ...
- 使用HANDLECOLLISIONS的几个场景
使用HANDLECOLLISIONS的几个场景: 1.target丢失delete记录(missing delete),忽略该问题并不记录到discardfile 2.target丢失update记录 ...
- tomcat动态查看服务器打印日志
[root@localhost ~]# cd /usr/local/tomcat/logs [root@localhost logs]# tail -f catalina.out FROM:htt ...
- 初学者指南:ZFS 是什么,为什么要使用 ZFS?
作者: John Paul 译者: LCTT Lv Feng 今天,我们来谈论一下 ZFS,一个先进的文件系统.我们将讨论 ZFS 从何而来,它是什么,以及为什么它在科技界和企业界如此受欢迎. 虽然我 ...
- ResNet(深度残差网络)
注:平原改为简单堆叠网络 一般x是恒等映射,当x与fx尺寸不同的时候,w作用就是将x变成和fx尺寸相同. 过程: 先用w将x进行恒等映射.扩维映射或者降维映射d得到wx.(没有参数,不需要优化器训练) ...