[CQOI2013]新Nim游戏

/**
 * Problem:Nim
 * Author:Shun Yao
 * Time:2013.5.28
 * Result:Accepted
 */

#include <cstdio>

void swap(long &x, long &y) {
	x ^= y;
	y ^= x;
	x ^= y;
}

int main() {
	long n, a[100], b[100];
	freopen("nim.in", "r", stdin);
	freopen("nim.out", "w", stdout);
	scanf("%ld", &n);
	for (long i = 0; i < n; ++i) {
		scanf("%ld", a + i);
		b[i] = a[i];
	}
	long now = -1;
	for (long i = 1 << 30; i; i >>= 1) {
		long pos = -1;
		for (long j = now + 1; j < n; ++j)
			if ((a[j] & i) && (pos < 0 || b[j] > b[pos]))
				pos = j;
		if (pos < 0)
			continue;
		++now;
		if (pos != now) {
			swap(a[pos], a[now]);
			swap(b[pos], b[now]);
		}
		for (long j = now + 1; j < n; ++j)
			if (a[j] & i)
				a[j] ^= a[now];
	}
	long long ans = 0;
	for (long i = 0; i < n; ++i)
		if (!a[i])
			ans += (long long)b[i];
	printf("%lld", ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

 又有错误: 注意c++ &引用 的用法.

 

posted @ 2013-05-28 16:25  hsuppr  阅读(281)  评论(0编辑  收藏  举报