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(矩阵乘法)的更多相关文章

  1. ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)

    We consider problems concerning the number of ways in which a number can be written as a sum. If the ...

  2. ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

    Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  3. ZOJ 2671 Cryptography 矩阵乘法+线段树

    B - Cryptography Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

  4. ZOJ 3256 Tour in the Castle 插头DP 矩阵乘法

    题解 这题是一道非常好的插头题,与一般的按格转移的题目不同,由于m很大,要矩阵乘法,这题需要你做一个按列转移的插头DP. 按列转移多少与按格转移不同,但大体上还是基于连通性进行转移.每一列只有右插头是 ...

  5. *HDU2254 矩阵乘法

    奥运 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  6. *HDU 1757 矩阵乘法

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  8. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  9. bzoj 2738 矩阵乘法

    其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...

随机推荐

  1. Unity 内置Shader变量、辅助函数等

    一:标准库里的常用.cginc文件 HLSLSupport.cginc - (automatically included) Helper macros and definitions for cro ...

  2. 汇编中中括号[]作用以及lea和mov指令的区别

    现在总结一下:其中牵扯到lea指令,mov指令,[] 一.lea指令:对于寄存器来说:第二个操作数是寄存器必须要加[],不然报错,这里lea就是取[寄存器]的值,如:mov eax,2lea ebx, ...

  3. 学习bootstrap

    菜鸟教程 bootstrap开发框架 伍华聪 Bootstrap——一款超好用的前端框架

  4. A string is a sequence

    A string is a sequence of characters. You can access the characters one at a time with the bracket o ...

  5. 在cncc的最后几天的笔记

    数据库范式:http://blog.csdn.net/fg2006/article/details/6936439 数据库事务隔离级别:http://blog.csdn.net/fg2006/arti ...

  6. springMVC中跳转问题

    在使用SpringMVC时遇到了这个跳转的问题很头疼.现在总结出来,对以后的开发有所帮助. . 1.可以采用ModelAndView: @RequestMapping("test1" ...

  7. Css border样式

    1 四个边框 border-left 设置左边框,一般单独设置左边框样式使用border-right 设置右边框,一般单独设置右边框样式使用border-top 设置上边框,一般单独设置上边框样式使用 ...

  8. http://www.open-open.com/lib/tag/ReactiveCocoa

    http://www.open-open.com/lib/tag/ReactiveCocoa

  9. Http协议与TCP协议理解(转载的)

    TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...

  10. WebService通讯技术的CXF框架问题

    WebService通讯技术的CXF框架问题 严重: Servlet /cxf_rs_spring threw load() exception java.lang.ClassCastExceptio ...