sgu197:Nice Patterns Strike Back(状压dp+矩阵乘法优化)
2015-01-18 14:24
459 查看
题意:
有一个N*M的棋盘,棋盘每个格子要么是黑要么是白,每个2*2的方块颜色不能都相同,求总的颜色种数mod P。
1<=N<=10^100, 1<=M<=5, 1<=P<=10000。
分析:
1<=M<=5很明显的状压dp,但是1<=N<=10^100是我们不能一行行dp,还得用矩阵乘法优化。
对于矩阵G,G[i][j]==1表示j状态可以转移到i状态,我们dfs出矩阵G,然后G^(n-1),接着就是水了。
有一个N*M的棋盘,棋盘每个格子要么是黑要么是白,每个2*2的方块颜色不能都相同,求总的颜色种数mod P。
1<=N<=10^100, 1<=M<=5, 1<=P<=10000。
分析:
1<=M<=5很明显的状压dp,但是1<=N<=10^100是我们不能一行行dp,还得用矩阵乘法优化。
对于矩阵G,G[i][j]==1表示j状态可以转移到i状态,我们dfs出矩阵G,然后G^(n-1),接着就是水了。
#include <cstdio> #define get(s, i) ((s>>(i-1))&1) using namespace std; const int MAXL = 109, MAXS = 1<<5; struct bi { short a[MAXL]; int l; void read() { char c = getchar(); if(c >= '0' && c <= '9') read(); else return ; a[++l] = c-'0'; } void minus(int k) { if(a[1] >= k) a[1] -= k; else { a[1] += 10-k; a[2]--; int i = 2; while(a[i] < 0) { a[i] += 10; a[++i]--; } for(int i = l; i >= 1; --i) if(a[i] != 0) {l = i;break;} } } bool empty() { if(l == 1 && a[1] == 0) return true; else return false; } void binary() { if(a[1]&1) a[1]--; for(int i = l; i >= 1; --i) { if(a[i]&1) a[i-1] += 10; a[i] >>= 1; } for(int i = l; i >= 1; --i) if(a[i] != 0) {l = i;break;} } }n; int m, mod; struct mtx { short g[MAXS][MAXS]; int n, m; }ini, conse, f; int ans; mtx operator * (const mtx a, const mtx b) { mtx tmp = {{0}, 0, 0}; tmp.n = a.n, tmp.m = b.m; for(int i = 0; i < a.n; ++i) for(int k = 0; k < a.m; ++k) for(int j = 0; j < b.m; ++j) tmp.g[i][j] = (tmp.g[i][j]+a.g[i][k]*b.g[k][j]%mod)%mod; return tmp; } void make(int ss, int s, int k) { if(k == m+1) { ini.g[ss][s] = 1; return ; } if(k == 1) { make(ss, s, k+1); make(ss+1, s, k+1); } else { int a = get(ss, k-1), b = get(s, k-1), c = get(s, k); if(a == b && b == c) make(ss+((a^1)<<(k-1)), s, k+1); else { make(ss+(1<<(k-1)), s, k+1); make(ss, s, k+1); } } } int main() { n.read();scanf("%d%d", &m, &mod); ini.n = ini.m = 1<<m;f.n = 1<<m, f.m = 1; for(int i = 0; i < (1<<m); ++i) { make(0, i, 1); f.g[i][0] = 1; } n.minus(1); conse.n = conse.m = 1<<m; for(int i = 0; i < conse.n; ++i) conse.g[i][i] = 1; while(!n.empty()) { if(n.a[1]&1) conse = conse*ini; ini = ini*ini; n.binary(); } f = conse*f; for(int i = 0; i < f.n; ++i) ans = (ans+f.g[i][0])%mod; printf("%d\n", ans); return 0; }
相关文章推荐
- SGU 197.Nice Patterns Strike Back
- Nice Patterns Strike Back(位运算dp+大数矩阵快速幂)
- zoj 2317 Nice Patterns Strike Back(矩阵乘法)
- sgu 197 Nice Patterns Strike Back 矩阵快速幂
- SGU 197 Nice Patterns Strike Back || ZOJ 2317 Nice Patterns Strike Back
- sgu 197 Nice Patterns Strike Back(构造矩阵快速幂)
- 【ZOJ】Nice Patterns Strike Back(矩阵快速乘法)
- ZOJ 2317 Nice Patterns Strike Back(矩阵快速幂)
- ACdream 1214 Nice Patterns Strike Back
- zoj 2317 Nice Patterns Strike Back
- ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
- zoj 2317 Nice Patterns Strike Back(矩阵乘法)
- ASC1 E Nice Patterns Strike Back
- 快速矩阵幂+DFS构造矩阵+大数 ACdream1214 Nice Patterns Strike Back
- ASC1 E-Nice Patterns Strike Back
- Acdream 1214 Nice Patterns Strike Back (矩阵乘法 + 状态压缩)
- ZOJ 2317 Nice Patterns Strike Back
- ZOJ-2317 Nice Patterns Strike Back
- ZOJ2317 Nice Patterns Strike Back
- HDU5808 Price List Strike Back