Game HDU - 3389 (Nim)

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

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堆石子,下标1~n,两个人玩游戏,轮流操作,每轮玩家可以任意选择两个堆石子下标为a, b,并且满足b < a;a > 0;(a + b) % 2 = 1;(a + b) % 3 = 1。玩家可以将a中任意石头(大于0)拿放到b中,Alice先手,不能操作者失败。

题解:当不能操作时,所以石头必当移动到1,3,4中,当列出表后就会知道,满足:

(i % 3 == 1 || i & 3 == 2) && (i / 3) % 2 == 0  的堆合并到1中;

(i % 3 == 1 || i & 3 == 2) && (i / 3) % 2 == 1  的堆合并到4中;

i % 3 == 0  的堆合并到3中。

我们假设现在只有一颗石头,当这颗石头需要移动奇数才能到达终点时,则先手必胜。

现在存在很多石头就是nim博弈,求个异或和就行了。

所以现在只需要计算每颗石头移动到终点需要步数的奇偶性就行了。

简化后的代码如下:

//#include
//#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a, b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
const LL INF  =  0x3f3f3f3f3f3f;
const int O    =  1e6;
const int mod  =  1e9+7;
const int maxn =  2e3 +5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const double eps = 1e-8;

int main(){
    int T, l = 0; cin >> T;
    while( T --) {
        int n; cin >> n;
        int ans = 0;
        for(int i=1; i<=n; i++ ) {
            int x; cin >> x;
            if(i % 3 == 2) ans ^= x;
            if(i % 3 == 0 && !(i % 2)) ans ^= x;
        }
        if(ans) printf("Case %d: Alice\n", ++l);
        else printf("Case %d: Bob\n", ++l);
}
    return 0;
}

 

你可能感兴趣的:(博弈论)