传送门:点击打开链接
题意:告诉你矩阵大小是n*m,要求矩阵中不能有2*2的白色子矩阵或者黑色子矩阵,最后种类数模P
思路:如果不是大数,这道题还是非常有意思的。。对于专门卡C++的题目也是醉了...因为n太大了,而m最大也只有5,很明显是大数上的快速矩阵幂。
问题是如何构造出矩阵出来,之前做过骨牌的题目,就是利用DFS来构造的,感觉这道题在思路上是一样的,同样也是利用DFS先构造出矩阵
然后直接大数+快速矩阵幂撸一发就行了
#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 * 10 + 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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-31 16:49:05