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
分析
题目大致意思就是说有n个框,每个框都是空的或包含多张卡片,从A框里取大于0的卡片移动到B框去(AB为框编号),并且满足B < A && (A + B) % 2 = 1 && (A + B) % 3 = 0;并且A(A框里面)不为空,第i个整数表示第i个方框中的卡数当最后一位不能移动的时候为输,Alice先手。
看到这里就已经很像Nim博弈了,回忆一下Nim博弈经典题目:有n堆若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜。 跑题了…题目说了每次移动都需要满足 B < A && (A + B) % 2 = 1 && (A + B) % 3 = 0,这个可以合并成为两个判断条件B < A && (A + B) % 6 == 3
现在来看看样例如何理解:
Case 1:
2
1 2
这里(A + B) % 6 == 3只有一种情况 A + B = 3
框编号 | 1 | 2 |
---|---|---|
第i个边框里的卡片数 | 1 | 2 |
很简单的移动 编号为2的移动到编号为1的
框编号 | 1 | 2 |
---|---|---|
第i个边框里的卡片数 | 3 | 0 |
再看样例2
Case 2:
7
1 3 3 2 2 1 2
此时(A + B) % 6 == 3有两种情况,A + B = 3或者9,可以明显的看出0+3,1+2,2+7,3+6,4+5;
框编号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
第i个边框里的卡片数 | 1 | 3 | 3 | 2 | 2 | 1 | 2 |
移动是
第一步:编号7——>编号2
框编号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
第i个边框里的卡片数 | 1 | 5 | 3 | 2 | 2 | 1 | 0 |
第二步:编号6——>编号3
框编号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
第i个边框里的卡片数 | 1 | 2 | 4 | 3 | 2 | 0 | 0 |
第三步:编号5——>编号4
框编号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
第i个边框里的卡片数 | 1 | 2 | 4 | 5 | 0 | 0 | 0 |
第四步:编号2——>编号1
框编号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
第i个边框里的卡片数 | 3 | 0 | 4 | 5 | 0 | 0 | 0 |
因为不能在移动了所以Bob胜。
可以很清楚的看到也可以很容易的总结出来
不管怎么移动,最后的终点就是1,3,4号,也就是说1,3,4号是不能够再转移卡片到其他框里的,其他框中的卡片经过若干步的转移最终也一定会转移到1,3,4号中去。
每个位置移到下个位置后奇偶性改变,模3结果也有固定变化(0->0,1->2,2->1),比如模3为1的奇数可以移到模3为2的偶数,再走一步又走到模3为1的奇数,但最终只可能移到模3为1的奇数,也就是1(因为1、3、4中没有模3为2的),其他类型的数的最终位置也可如此得到。由于移到的最终位置(1 or 3 or 4)是固定的,本身的奇偶性和模3结果也是固定的,那么移动步数的奇偶性也是固定的。
再看题目之前我们已经合并成为两个条件了B < A && (A + B) % 6 = 3,就可以很容易的模出来,模6为0、2、5的位置移动步数为奇,其余为偶。既n % 6 = 0,2,5时到达终点1,3,4时总是会走奇数步,其他为偶数步。
贴一张图可以很清楚的明白了
图片转载
接下来就很容易的证明出来,偶数步无关紧要,因为就算偶数步移了,在下一步我可以把对方的操作往后移一步,而奇数步可以一下子就达到1,3,4的状态。
这就可以直接把奇数步的位置异或一下就行了
代码如下
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e4 + 5;
int a[maxn];
bool cmp(int a, int b)
{
return a < b;
}
int main()
{
int ans = 0;
int t , n;
cin >> t;
while (t--)
{
int step = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (i % 6 == 0 || i % 6 == 2 || i % 6 == 5)
step ^= a[i];
}
if (step)
cout <<"Case " << ++ans << ": " << "Alice" << endl;
else
cout <<"Case " << ++ans << ": " << "Bob" << endl;
}
return 0;
}