ASC1 E Nice Patterns Strike Back
2015-03-21 22:58
615 查看
题意:给你N×(1-5)的格子,每一个格子有两种颜色,其中2x2个格子内的颜色不能都相同。
解题思路:状态压缩+ 矩阵快速幂 +大数。
解题代码:
View Code
解题思路:状态压缩+ 矩阵快速幂 +大数。
解题代码:
// File Name: e.cpp // Author: darkdream // Created Time: 2015年03月21日 星期六 19时58分58秒 #include<vector> #include<list> #include<map> #include<set> #include<deque> #include<stack> #include<bitset> #include<algorithm> #include<functional> #include<numeric> #include<utility> #include<sstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<ctime> #define LL long long using namespace std; int M , n ; struct Matrix { int mat[40][40]; void clear() { memset(mat,0,sizeof(mat)); } void output() { for(int i = 0 ;i < n;i ++) { for(int j = 0 ;j < n;j ++) { printf("%d ",mat[i][j]); } printf("\n"); } } Matrix operator *(const Matrix &b) const { Matrix ret; ret.clear(); for(int i = 0 ;i < n;i ++) for(int j = 0 ;j < n;j ++) { for(int s = 0 ; s < n; s ++) { ret.mat[i][j] = (ret.mat[i][j] + mat[i][s] *b.mat[s][j] % M) % M; } } return ret; } }a; char str[1000]; int num[1000]; int len ; int t; void judge(int x, int y) { //printf("%d %d***\n",x,y); int tx = x; int ty = y; int ttt = 0 ; int txa[10]; int tya[10]; memset(txa,0,sizeof(txa)); memset(tya,0,sizeof(tya)); while(tx) { txa[ttt] = tx % 2; tx/= 2; ttt++; } ttt =0 ; while(ty) { tya[ttt] = ty % 2; ty /= 2; ttt++; } for(int i = 1;i < t; i ++) if(tya[i]+tya[i-1]+txa[i] + txa[i-1] == 4 || tya[i] + tya[i-1] + txa[i] + txa[i-1] == 0 ) { return; } a.mat[x][y] = 1; } void jian() { for(int i= 0 ;i <= len/2;i ++) { swap(num[i],num[len-i]); } for(int i = 0 ;i <= len ;i ++) { if(num[i] == 0 ) { num[i] = 9 ; }else{ num[i]-- ; break; } } /*for(int i = len ;i >= 0 ;i --) printf("%d",num[i]); printf("\n");*/ if(num[len] == 0 ) len --; /* for(int i = len ;i >= 0 ;i --) printf("%d",num[i]); printf("\n");*/ } void chu() { int tmp = 0 ; for(int i = len ;i >= 0 ;i --) { int k = (tmp * 10 + num[i]); num[i] = k/2; tmp = k % 2; } if(num[len] == 0 ) len --; /* for(int i = len ;i >= 0 ;i --) printf("%d",num[i]); printf("\n");*/ } Matrix Pow(Matrix a) { Matrix ret; ret.clear(); for(int i= 0 ;i < n;i ++) ret.mat[i][i] = 1; Matrix tmp = a; while(len != -1) { // printf("%d ***\n",len); if(num[0] % 2 == 1) { ret = ret * tmp ; //ret.output(); } tmp = tmp*tmp ; chu(); } return ret; } int main(){ freopen("nice.in","r",stdin); freopen("nice.out","w",stdout); scanf("%s %d %d",str,&t,&M); len = strlen(str); for(int i= 0 ;i < len ;i ++) { num[i] = str[i] - '0'; } len --; n = (1 << t); for(int i = 0 ;i <n ;i ++) { for(int j = 0 ;j < n;j ++) { judge(i,j); } } // a.output(); jian(); a = Pow(a); //a.output(); int sum = 0 ; for(int i = 0 ;i < n;i ++) for(int j = 0 ;j < n;j ++) { sum = (sum + a.mat[i][j]) % M; } printf("%d\n",sum); return 0; }
View Code
相关文章推荐
- Nice Patterns Strike Back(位运算dp+大数矩阵快速幂)
- zoj 2317 Nice Patterns Strike Back
- ZOJ2317 Nice Patterns Strike Back
- Acdream 1214 Nice Patterns Strike Back (矩阵乘法 + 状态压缩)
- ZOJ 2317 Nice Patterns Strike Back
- ASC1 E-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
- sgu197:Nice Patterns Strike Back(状压dp+矩阵乘法优化)
- ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
- SGU 197.Nice Patterns Strike Back
- zoj 2317 Nice Patterns Strike Back(矩阵乘法)
- 快速矩阵幂+DFS构造矩阵+大数 ACdream1214 Nice Patterns Strike Back
- sgu 197 Nice Patterns Strike Back(构造矩阵快速幂)
- zoj 2317 Nice Patterns Strike Back(矩阵乘法)
- SGU 197 Nice Patterns Strike Back || ZOJ 2317 Nice Patterns Strike Back
- ZOJ 2317 Nice Patterns Strike Back(矩阵快速幂)
- URAL - 2029 Towers of Hanoi Strike Back