Partitioning Game(博弈论-sg函数打表)
judge:LightOJ-1214
Time limit 4000 ms
Memory limit 32768 kB
OS Linux
描述
Alice and Bob are playing a strange game. The rules of the game are:
-
Initially there are n piles.
-
A pile is formed by some cells.
-
Alice starts the game and they alternate turns.
LightOJ-1214
Input
Sample Input
3
1
4
3
1 2 3
1
7
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Bob
题意
有n堆石子,每回合可选择把其中一堆分成 数量不同 的两堆,且必须分一次。不能再分的玩家输。
sg函数打表。
代码
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e3+5;
const int mod = 1e9+7;
const int maxn = 10005;
int sg[maxn], mex[maxn];
void getsg() {
sg[0] = sg[1] = sg[2] = 0;
for(int i = 3; i <= 10000; i++){
memset(mex, 0, sizeof(mex));
for(int j = 1; j * 2 < i; j++){
mex[sg[j] ^ sg[i - j]] = 1;
}
int pos = 0;
while(mex[pos] != 0) pos++;
sg[i] = pos;
}
}
int main() {
//freopen("in.txt", "r", stdin);
getsg();
int t, n;
cin >> t;
int k = 0;
while(t --) {
cin >> n;
int x;
int ans;
for(int i = 1; i <= n; ++ i) {
cin >> x;
if(i == 1)
ans = sg[x];
else
ans ^= sg[x];
}
printf("Case %d: ", ++ k);
if(ans)
cout << "Alice" << endl;
else
cout << "Bob" << endl;
}
return 0;
}