usaco6.5.1 All Latin Squares

17 篇文章 0 订阅

一 原题

All Latin Squares

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  4
is 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.......N
Your 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阵数量。

一个很强力的剪枝,对于下面两种情况:这两种情况的方案数应当相同。解释如下:

情况一
12345
21xxx
34xxx
45xxx
53xxx
情况二
12345
23xxx
31xxx
45xxx
54xxx

判定情况等价的依据:考虑前两列形成的循环节,情况一中循环节为(2, 1), (4, 5, 3);情况二中循环节为(2, 3, 1), (5,  4)。这两个循环节同构(数量相等,排序后每个循环节大小一样)。

证明方案数相等:对于情况二的一组合法解,规定置换规则:2->4, 3->5, 1->3, 5->2, 4->1,那么情况二变为:

34512
45xxx
53xxx
12xxx
21xxx

按照第一列排序得到:

12xxx
21xxx
34512
45xxx
53xxx
这种情况的方案数等于情况一的方案数。同理,情况一可以经过类似置换,得到情况二的解。


三 代码

运行结果:

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 yoursubmission #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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值