Portal: Click to open link
Test instructions: tells you that the matrix size is n*m, requires the matrix can not have the 2*2 white sub-matrix or the black sub-matrix, the last kind of number modulo p
Idea: If it's not a large number, the problem is still very interesting. For the special card C + + topic is also drunk ... Because N is too big, and M Max is only 5, it is obviously the fast matrix power on large numbers.
The problem is how to construct the matrix out, before the topic of Domino, is the use of Dfs to construct, feel the problem in the thinking is the same, the same is the use of DFS first constructs the matrix
And then the direct large number + fast matrix powers the line.
#include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iostream># include<algorithm> #include <functional> #define FIN freopen ("Input.txt", "R", stdin) using namespace std; const int MATMX = 50;const int dlen = 4;const int MAXN = 9999;class BN {public:int a[500]; int Len; BN (const int b = 0) {int c, d = b; len = 0; memset (A, 0, sizeof (a)); while (d > maxn) {c = d-(d/(MAXN + 1)) * (MAXN + 1); D = d/(MAXN + 1); a[len++] = c; } a[len++] = D; } BN (const char *s) {int T, K, index, L, I; memset (A, 0, sizeof (a)); L = strlen (s); len = L/dlen; if (L% dlen) len++; index = 0; for (i = L-1; I >= 0; I-= Dlen) {t = 0; K = I-dlen + 1; if (k < 0) k = 0; for (int j = k; J <= I; j + +) {t = T * + s[j]-' 0 '; } a[index++] = t; }} bn operator/(const int &b) const {BN ret; int I, down = 0; for (int i = len-1; I >= 0, i--) {ret.a[i] = (A[i] + down * (MAXN + 1))/b; Down = a[i] + down * (MAXN + 1)-ret.a[i] * b; } Ret.len = Len; while (ret.a[ret.len-1] = = 0 && ret.len > 1) ret.len--; return ret; } bool Operator> (const BN &t) const {INT ln; if (len > T.len) return true; else if (len = = t.len) {ln = len-1; while (a[ln] = = T.a[ln] && ln >= 0) ln--; if (ln >= 0 && A[ln] > T.a[ln]) return true; else return false; } else return false; } BN operator-(const bn &t) const {int I, j, big; BOOL Flag; BN T1, T2; if (*this > T) {t1 = *this; t2 = T; Flag = 0; } else {t1 = T; t2 = *this; flag = 1; } big = T1.len; for (i = 0; i < big; i++) {if (T1.a[i] < T2.a[i]) {j = i + 1; while (t1.a[j] = = 0) j + +; t1.a[j--]--; while (J > i) t1.a[j--] + = MAXN; T1.a[i] + = MAXN + 1-t2.a[i]; } else t1.a[i]-= t2.a[i]; } T1.len = big; while (t1.a[t1.len-1] = = 0 && t1.len > 1) {t1.len--; big--; } if (flag) t1.a[big-1] = 0-t1.a[big-1]; return t1; } int operator% (const int &b) const {int I, d = 0; for (int i = len-1; I >= 0; i--) {d = ((d * (MAXN + 1)% B + a[i])% B; } return D; }}; LL mod;struct Mat {int m, n; LL S[MATMX][MATMX]; Mat (int A, int b) {m = A; n = b; memset (s, 0, sizeof (s)); } MaT (int A, int B, LL w[][matmx]) {m = A; n = b; for (int i = 0, i < m; i++) {for (int j = 0; J < N; j + +) {S[i][j] = w[i][j]; } } }}; Mat Mat_mul (Mat A, Mat B) {Mat C (a.m., B.N); for (int i = 0; i < a.m.; i++) {for (int j = 0; J < B.N; J + +) {for (int k = 0; k < A.N; k++) { C.S[I][J] = (C.s[i][j] + a.s[i][k] * b.s[k][j])% MoD; }}} return C;} Mat Blank (int m, int n) {mat ret (m, n); for (int i = 0; i < m; i++) {ret. S[i][i] = 1; } return ret;} Mat Mat_pow (Mat A, BN b) {MAT ret = Blank (a.m., A.N); while (b > 0) {if (b% 2) ret = Mat_mul (ret, A); A = Mat_mul (A, a); b = B/2; } return ret;} int m; LL tb[matmx][matmx];void DFS (int x, int y, int p) {if (p = = m) {Tb[x][y] = 1; Return DFS (x << 1 | 1, y << 1, p + 1); DFS (x << 1, y << 1 | 1, p + 1); if (!P | | (! ((x & 1) && (Y & 1)))) DFS (x << 1 | 1, y << 1 | 1, p + 1); if (!p | | (! (! (x & 1) &&! (Y & 1)))) DFS (x << 1, y << 1, p + 1);} int main () {//fin; Char word[500]; while (~SCANF ("%s%d%lld", Word, &m, &mod)) {memset (TB, 0, sizeof (TB)); DFS (0, 0, 0); bn n = bn (word); Mat s (1 << m, 1 << m, TB), fuck (1 << m, 1); for (int i = 0; i < (1 << m); i++) {fuck. S[i][0] = 1; } Mat res = Mat_mul (Mat_pow (S, n-1), fuck); LL ans = 0; for (int i = 0; i < (1 << m); i++) {ans = (ans + res). S[I][0])% MoD; } printf ("%lld\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fast matrix Power +dfs Construction Matrix + large number ACdream1214 nice Patterns Strike back