NC19042. Pattern
描述
输入描述
The first line of input contains an integer t which is the number of test cases. Then t test cases follow. For each test case, the first line consists of two integers N(N ≤ 66), and M(M ≤ 6). The i-th line of the next N lines contains M integers, and among them the j-th integer donates the point (i,j). If the point is panted color ci, the integer is i, otherwise it is −1.
输出描述
For each test cases, output modulo 10007.
示例1
输入:
2 2 2 -1 -1 -1 -1 3 3 1 1 1 1 0 1 1 1 1
输出:
18 4
说明:
C++11(clang++ 3.9) 解法, 执行用时: 398ms, 内存消耗: 35292K, 提交时间: 2018-11-22 14:35:34
#include <cstdio> #include <cstring> #include <ctime> const int MAXN = 10; const int MAXM = 100; const int MAXS = 17000; const int MOD = 10007; int E2[MAXN * 2]; int T[6][16][20]; void prework() { for (int i = 0; i < MAXN * 2; ++i) E2[i] = (1 << i); memset(T, 0, sizeof(T)); for (int i = 0; i < 16; ++i) for (int j = 0; j < 16; ++j) { int k1 = ((i >> 0) & 1) + ((i >> 2) & 1) + ((j >> 0) & 1) + ((j >> 2) & 1); int k2 = ((i >> 1) & 1) + ((i >> 3) & 1) + ((j >> 1) & 1) + ((j >> 3) & 1); if (k1 == k2) { T[k1][i][++T[k1][i][0]] = j; T[5][i][++T[5][i][0]] = j; } } } int n, m; int a[MAXM][MAXN]; int f[68][8][16388]; void init() { scanf("%d %d", &m, &n); memset(a, 0, sizeof(a)); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) { scanf("%d", &a[i][j]); if (a[i][j] == -1) a[i][j] = 5; } } void solve() { int s = E2[(n + 1) << 1]; memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < s; ++k) if (f[i][j][k] != 0) { int cur = (((k >> j) >> j) & 15); for (int ti = 1; ti <= T[a[i][j]][cur][0]; ++ti) { int next = (k ^ (((cur ^ T[a[i][j]][cur][ti])<<j)<<j)); f[i][j+1][next] = (f[i][j+1][next] + f[i][j][k]) % MOD; } } } for (int k = 0; k < s; ++k) if (f[i][n][k] != 0 && (((k >> n) >> n) == 0)) { int next = ((k << 2) & (s - 1)); f[i+1][0][next] = (f[i+1][0][next] + f[i][n][k]) % MOD; } } printf("%d\n", f[m][0][0]); } int main() { //freopen("in.txt", "r", stdin); prework(); int tt; scanf("%d", &tt); while (tt--) { init(); solve(); } fprintf(stderr,"%.2f s\n",clock()*1.0/CLOCKS_PER_SEC); return 0; }