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

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写了。

  1 import java.awt.Checkbox;
  2 import java.io.BufferedInputStream;
  3 import java.io.BufferedOutputStream;
  4 import java.io.PrintWriter;
  5 import java.math.BigInteger;
  6 import java.util.Scanner;
  7
  8 public class Main {
  9
 10     static int p;
 11
 12     public static class Matrix implements Cloneable {
 13         long[][] a;
 14         int d;
 15
 16         public Matrix(int d) {
 17             this.d = d;
 18             a = new long[d][d];
 19         }
 20
 21         public Matrix multiply(Matrix m) {
 22             Matrix ret = new Matrix(d);
 23             for (int i = 0; i < d; ++i) {
 24                 for (int j = 0; j < d; ++j) {
 25                     for (int k = 0; k < d; ++k) {
 26                         ret.a[i][j] += a[i][k] * m.a[k][j];
 27                         ret.a[i][j] %= p;
 28                     }
 29                 }
 30             }
 31             return ret;
 32         }
 33
 34         public Matrix clone() {
 35             Matrix ret = new Matrix(d);
 36             ret.a = a.clone();
 37             return ret;
 38         }
 39
 40         Matrix pow(BigInteger cnt) {
 41             // 先生成一个单位矩阵
 42             Matrix eye = new Matrix(d);
 43             for (int i = 0; i < d; i++)
 44                 eye.a[i][i] = 1;
 45
 46             for (int i = cnt.bitLength() - 1; i >= 0; i--) {
 47                 eye = eye.multiply(eye);
 48                 if (cnt.testBit(i)) {
 49                     eye = eye.multiply(this);
 50                 }
 51             }
 52             return eye;
 53         }
 54     }
 55
 56     static boolean check(int x, int y, int m) {
 57         for (int i = 1; i < m; i++) {
 58             if ((x & 3) == (y & 3) && (x & 1) == ((x & 2) >> 1)) {
 59                 return false;
 60             }
 61             x >>= 1;
 62             y >>= 1;
 63         }
 64
 65         return true;
 66     }
 67
 68     public static void main(String[] args) {
 69
 70         Scanner cin = new Scanner(new BufferedInputStream(System.in));
 71         PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
 72
 73         int T = cin.nextInt();
 74
 75         while (T-- != 0) {
 76             BigInteger n = cin.nextBigInteger();
 77             int m = cin.nextInt();
 78             p = cin.nextInt();
 79
 80             // 生成矩阵A
 81             int d = (1 << m);
 82             Matrix A = new Matrix(d);
 83             for (int i = 0; i < d; i++)
 84                 for (int j = 0; j < d; j++) {
 85                     if (check(i, j, m))
 86                         A.a[i][j] = 1;
 87                 }
 88
 89             A = A.pow(n.subtract(BigInteger.ONE));
 90
 91             long ans = 0;
 92             for (int i = 0; i < d; i++)
 93                 for (int j = 0; j < d; j++) {
 94                     ans = (ans + A.a[i][j]) % p;
 95                 }
 96
 97             cout.println(ans);
 98             if (T != 0)
 99                 cout.println("");
100             // System.out.println(ans);
101         }
102
103         cin.close();
104         cout.close();
105
106     }
107 }
时间: 2024-10-14 04:46:21

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

ASC(1)E(矩阵快速幂+简单DP)

Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description You might have noticed that there is the new fashion among rich people to have their yards til

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...