HDU 3389 Game (阶梯博弈)
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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
Source
The 5th Guangting Cup Central China Invitational Programming Contest
题意:有t组数据。每组数据有n个盒子,这n个盒子编号为12345678......。(注意不是从0开始的)
每个盒子中有一定量的卡片。每次取编号为B和编号为A的盒子, 要求满足
B<A && (A+B)%2=1 && (A+B)%3=0,把A中的任意数量的卡片转移给B,谁不能再转移了谁输。
题解:阶梯博弈,只需要考虑步数为奇数的盒子,步数为偶数的盒子不需要考虑。
在本题中编号为1,3,4的盒子不能转移卡片,其余盒子均可转移。例如:
2->1, 5->4, 6->3, 7->2 ,8->1, 9->6...
其本质为有n级阶梯,我们在%3的余数中进行转移0->0, 1->2, 2->1;最后的结果
一定是1或者3或者4,这些盒子中卡片转移的步数的奇偶性是一定的。为什么这么说呢?
因为即使有些盒子例如编号11的盒子,有11->4和11->10->8->1两种选择,但是这
两种选择的步数的奇偶性是相同的,都是奇数,所以奇偶性是一定的。
所以我们把这个阶梯博弈转化为尼姆博弈就行了,对步数为奇数的盒子进行尼姆博弈。
在纸上多写几个数或者用打表的方法可以发现如下规律:
盒子编号模6为0,2,5的位置的移动步数为奇,其余为偶。
推到这里就很好实现了。
#include <iostream> #include <cstdio> using namespace std; int main() { int t,cas=1; cin>>t; while(t--) { int n,data,ans=0; cin>>n; for(int i=1;i<=n;i++) { cin>>data; if(i%6==2||i%6==5||i%6==0) ans^=data; } if(ans) printf("Case %d: Alice\n",cas++); else printf("Case %d: Bob\n",cas++); } return 0; }