数论练习2:K - Georgia and Bob(POJ 1704) SG函数运用:尼姆博奕(Nimm Game)

Description

Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ..., and place N chessmen on different grids, as shown in the following figure for example: 

Georgia and Bob move the chessmen in turn. Every time a player will choose a chessman, and move it to the left without going over any other chessmen or across the left edge. The player can freely choose number of steps the chessman moves, with the constraint that the chessman must be moved at least ONE step and one grid can at most contains ONE single chessman. The player who cannot make a move loses the game. 

Georgia always plays first since "Lady first". Suppose that Georgia and Bob both do their best in the game, i.e., if one of them knows a way to win the game, he or she will be able to carry it out. 

Given the initial positions of the n chessmen, can you predict who will finally win the game? 

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case contains two lines. The first line consists of one integer N (1 <= N <= 1000), indicating the number of chessmen. The second line contains N different integers P1, P2 ... Pn (1 <= Pi <= 10000), which are the initial positions of the n chessmen.

Output

For each test case, prints a single line, "Georgia will win", if Georgia will win the game; "Bob will win", if Bob will win the game; otherwise 'Not sure'.

Sample Input

2
3
1 2 3
8
1 5 6 7 9 12 14 17

Sample Output

Bob will win
Georgia will win

这题太悲惨了,6WA了1RE,刚开始还没真正理解SG函数,然后WA了两次,然后又看PPT,有点理解后再WA几次,怎么找都找不出错误,最后才发现是排序那错了发火发火,数组从1开始的我排序确从0开始,唉……大意啊……可惜了……看就是博弈问题,不过得转化一下才得,刚开始没理解SG函数的具体……然后直接套模板直接WA,然后才知道。

因为只要左边的动,右边的跟着动就得了,动多少格都一样就行了,所以两个一起chessman之间的距离作为石头的个数,然后就相当于取多少石头最后没有取为此,SG为0就是第一个取的输,反之……所以如果n为偶数就正好可以两两配对了,如果为奇数就让第一个与0配对(即与最左边的边界配对,反正不能超过左边的边界)……

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
using namespace std;
int a[1050];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,sg=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        if(n&1)
        {
            for(sg^=a[1]-1,i=3;i<=n;i+=2)
                sg^=(a[i]-a[i-1]-1);
        }
        else
        {
            for(i=2;i<=n;i+=2)
                sg^=(a[i]-a[i-1]-1);
        }
        if(sg==0) puts("Bob will win");
        else puts("Georgia will win");
    }
    return 0;
}


你可能感兴趣的:(数论练习2:K - Georgia and Bob(POJ 1704) SG函数运用:尼姆博奕(Nimm Game))