一 原题
A square arrangement of numbers
1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4is a 5 x 5 Latin Square because each whole number from 1 to 5 appears once and only once in each row and column.
Write a program that will compute the number of NxN Latin Squares whose first row is:
1 2 3 4 5.......NYour program should work for any N from 2 to 7.
PROGRAM NAME: latin
INPUT FORMAT
One line containing the integer N.SAMPLE INPUT (file latin.in)
5
OUTPUT FORMAT
A single integer telling the number of latin squares whose first row is 1 2 3 . . . N.SAMPLE OUTPUT (file latin.out)
1344
二 分析
给定正整数n,n最大为7,要求构造n*n的矩阵,叫Latin阵。矩阵内的元素为[1, n]中的数,第一行从左到右为1、2、3...n,要求每一行,每一列都不含有重复的元素。要求输出全部可行解的个数。
思路肯定是搜索,有一个剪枝是很直观的,题目要求了第一行是1-n,我们可以规定第一列也是1-n,这样得到的方案数乘以(n-1)!就是最终答案。
但最后n=7时,最终答案超过10^10,即使除去6!=720,遍历所有的合法解也会超时,因此需要进一步剪枝。明确一下现在的问题:求所有第一行是1-n,第一列也是1-n的所有Latin阵数量。
一个很强力的剪枝,对于下面两种情况:这两种情况的方案数应当相同。解释如下:
1 | 2 | 3 | 4 | 5 |
2 | 1 | x | x | x |
3 | 4 | x | x | x |
4 | 5 | x | x | x |
5 | 3 | x | x | x |
1 | 2 | 3 | 4 | 5 |
2 | 3 | x | x | x |
3 | 1 | x | x | x |
4 | 5 | x | x | x |
5 | 4 | x | x | x |
判定情况等价的依据:考虑前两列形成的循环节,情况一中循环节为(2, 1), (4, 5, 3);情况二中循环节为(2, 3, 1), (5, 4)。这两个循环节同构(数量相等,排序后每个循环节大小一样)。
证明方案数相等:对于情况二的一组合法解,规定置换规则:2->4, 3->5, 1->3, 5->2, 4->1,那么情况二变为:
3 | 4 | 5 | 1 | 2 |
4 | 5 | x | x | x |
5 | 3 | x | x | x |
1 | 2 | x | x | x |
2 | 1 | x | x | x |
按照第一列排序得到:
1 | 2 | x | x | x |
2 | 1 | x | x | x |
3 | 4 | 5 | 1 | 2 |
4 | 5 | x | x | x |
5 | 3 | x | x | x |
三 代码
运行结果:
USER: Qi Shen [maxkibb3] TASK: latin LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4300 KB] Test 2: TEST OK [0.000 secs, 4300 KB] Test 3: TEST OK [0.000 secs, 4300 KB] Test 4: TEST OK [0.000 secs, 4300 KB] Test 5: TEST OK [0.000 secs, 4300 KB] Test 6: TEST OK [0.448 secs, 4300 KB] All tests OK.
Your program ('latin') produced all correct answers! This is your submission #4 for this problem. Congratulations!
AC代码:
/* ID:maxkibb3 LANG:C++ PROB:latin */ #include<fstream> #include<iostream> #include<map> #include<set> #include<cstring> using namespace std; #define LL long long #define mp make_pair ifstream fin("latin.in"); ofstream fout("latin.out"); int n, s[10][10]; bool row[10][10], column[10][10]; // recurring period related int label[10]; map<int, LL> hash_table; int get_hash() { memset(label, 0, sizeof(label)); int l = 1, ret = 0; set<int> periods; for(int i = 0; i < n; i++) { if(label[i]) continue; label[i] = l; int x = i, len = 1; while(label[s[x][1]] != l) { label[s[x][1]] = l; x = s[x][1]; len++; } periods.insert(len); l++; } for(set<int>::iterator it = periods.begin(); it != periods.end(); it++) { ret = ret * 10 + *it; } return ret; } LL dfs(int r, int c) { if(c == n - 1) { return 1; } LL ret = 0, delta; int hashval; for(int i = 0; i < n; i++) { if(row[r][i] || column[c][i]) { continue; } row[r][i] = column[c][i] = true; s[r][c] = i; if(r == n - 1 && c == 1) { hashval = get_hash(); map<int, LL>::iterator it = hash_table.find(hashval); if(it != hash_table.end()) { ret += it->second; row[r][i] = column[c][i] = false; continue; } } if(r == n - 1) { delta = dfs(1, c + 1); } else { delta = dfs(r + 1, c); } if(r == n - 1 && c == 1) { hash_table.insert(mp(hashval, delta)); } ret += delta; row[r][i] = column[c][i] = false; } return ret; } int main() { fin >> n; for(int i = 0; i < n; i++) { row[0][i] = column[i][i] = true; row[i][i] = column[0][i] = true; } LL ans = dfs(1, 1); for(int i = 2; i <= n - 1; i++) { ans *= i; } fout << ans << endl; return 0; }