3105: [cqoi2013]新Nim游戏
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1562 Solved: 918
[ Submit][ Status][ Discuss]
Description
传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同)。两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴。可以只拿一根,也可以拿走整堆火柴,但不能同时从超过一堆火柴中拿。拿走最后一根火柴的游戏者胜利。
本题的游戏稍微有些不同:在第一个回合中,第一个游戏者可以直接拿走若干个整堆的火柴。可以一堆都不拿,但不可以全部拿走。第二回合也一样,第二个游戏者也有这样一次机会。从第三个回合(又轮到第一个游戏者)开始,规则和Nim游戏一样。
如果你先拿,怎样才能保证获胜?如果可以获胜的话,还要让第一回合拿的火柴总数尽量小。
Input
第一行为整数k。即火柴堆数。第二行包含k个不超过109的正整数,即各堆的火柴个数。
Output
输出第一回合拿的火柴数目的最小值。如果不能保证取胜,输出-1。
Sample Input
6
5 5 6 6 5 5
5 5 6 6 5 5
Sample Output
21
HINT
k<=100
Source
#include<queue> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<cmath> using namespace std; typedef long long LL; const int INF = 2147483647; const int maxn = 200010; int n,b[maxn]; LL a[maxn],ans; inline LL getint() { LL ret = 0,f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') ret = ret * 10 + c - '0',c = getchar(); return ret * f; } inline LL insert(LL x) { for (int i = 31; i >= 0; i--) { if (!((1ll << i) & x)) continue; if (!b[i]) { b[i] = x; break; } x ^= b[i]; } return x; } inline bool cmp(int a,int b) { return a > b; } int main() { n = getint(); for (int i = 1; i <= n; i++) a[i] = getint(); sort(a + 1,a + n + 1,cmp); for (int i = 1; i <= n; i++) if (!insert(a[i])) ans += a[i]; printf("%lld",ans); return 0; }