Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 610 Accepted Submission(s): 426
Problem Description
Bob and Alice are playing a new game. There are n boxes which have been numbered from 1 to n. Each box is either empty or contains several cards. Bob and Alice move the cards in turn. In each turn the corresponding player should choose a non-empty box A and choose another box B that B<A && (A+B)%2=1 && (A+B)%3=0. Then, take an arbitrary number (but not zero) of cards from box A to box B. The last one who can do a legal move wins. Alice is the first player. Please predict who will win the game.
Input
The first line contains an integer T (T<=100) indicating the number of test cases. The first line of each test case contains an integer n (1<=n<=10000). The second line has n integers which will not be bigger than 100. The i-th integer indicates the number of cards in the i-th box.
Output
For each test case, print the case number and the winner's name in a single line. Follow the format of the sample output.
Sample Input
2 2 1 2 7 1 3 3 2 2 1 2
Sample Output
Case 1: Alice Case 2: Bob
Author
hanshuai@whu
Source
Recommend
notonlysuccess
题意:
游戏规则就是把卡片移动到更靠左的盒子中,A B 两个盒子满足(A+B) % 3 == 0 并且满足A、B奇偶性质不同。
思路:
将每个盒子看作分开的石子堆,考察每一类盒子向左侧放卡片的策略。 发现最终卡片会被放到1、3、4三个盒子中,即最终结果。故经过奇数步骤到达这三个盒子中的必然是胜点,经过偶数步骤到达这些盒子的是必败情况。必败情况的SG函数值是0,对异或不会产生影响。
分情况 奇数: %3 == 0 偶数步骤后到达盒子3
%3 == 1 偶数步骤后到达盒子1
%3 == 2 ji步骤后到达盒子2
偶数: %3 == 0 奇数步骤后到达盒子3
%3 == 1 偶数步骤后到达盒子1
%3 == 2 奇数步骤后到达盒子4
偶数步骤到达最终结果的情况均可以忽略不计,剩下的情况可以想象成N多组尼姆游戏,不断异或即可得到答案。
#include <iostream>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
int main()
{
int casen, res, num, i, temp, countn = 1;
scanf("%d", &casen);
while(casen--){
scanf("%d", &num);
res = 0;
for(i = 1; i <= num; i++){
scanf("%d", &temp);
if(i % 2 == 1)
if(i % 3 == 2) res = res xor temp;
if(i % 2 == 0){
if(i % 3 == 2) res = res xor temp;
if(i % 3 == 0) res = res xor temp;
}
}
if(res != 0)
printf("Case %d: Alice\n", countn++);
else
printf("Case %d: Bob\n", countn++);
}
return 0;
}