题目
Alice and Bob are playing a strange game. The rules of the game are:
1. Initially there are n piles.
2. A pile is formed by some cells.
3. Alice starts the game and they alternate turns.
4. In each tern a player can pick any pile and divide it into two unequal piles.
5. If a player cannot do so, he/she loses the game.
Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.
Output
For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.
样例
Sample Input
3
1
4
3
1 2 3
1
7
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Bob
算一个sg函数就好了!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
typedef long long ll;
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 10005;
int sg[maxn];
int mex[maxn];
void getsg(){
for (int i = 1; i <= maxn; i++){
memset(mex, false, sizeof(mex));
for (int j = 1; j * 2 < i; j++){
if (i != j * 2) mex[sg[j] ^ sg[i - j]] = 1;
}
for (int j = 0; j < maxn; j++){
if (!mex[j]){
sg[i] = j;
break;
}
}
}
return;
}
int main() {
int T, cnt = 0;
getsg();
cin >> T;
while (T--) {
int n, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
ans ^= sg[m];
}
cout << "Case " << ++cnt << ": ";
if (ans) cout << "Alice" << endl;
else cout << "Bob" << endl;
}
}
本文介绍了一款涉及策略和博弈的游戏,通过分析游戏规则,使用SG函数来预测Alice和Bob在最优策略下游戏的胜者。输入包含多个测试案例,每个案例由一堆细胞构成的堆开始,玩家轮流将堆分成两个不等的堆,无法操作者输。文章提供了C++代码实现,用于计算SG函数并确定胜者。
185

被折叠的 条评论
为什么被折叠?



