http://acm.hdu.edu.cn/showproblem.php?pid=1907

Nim的变形,最后取为败,详见http://www.cnblogs.com/tanky_woo/archive/2010/08/20/1804464.html中取火柴游戏2。

这里总结下结论:

1. 当所有堆个数都为1时,若为奇数堆,即异或不为0,则先手败,反之先手胜。

2. 非所有堆个数都为1时,则异或不为0先手胜。

用上述博文中的定义则为T0, S1, S2为必胜态,T1, S0为必败态。

code:

#include<cstdio>
int main(){
    int t, n, f, i, a, sum ;
    scanf("%d", &t) ;
    while(t--){
        sum = f = 0 ;
        scanf("%d", &n) ;
        for(i=0; i<n; i++){
            scanf("%d", &a) ;
            sum ^= a ;
            if(a>1) f = 1 ;
        }
        if(sum) sum = 1 ;
        if(f^sum)   printf("Brother\n") ;
        else        printf("John\n") ;
    }
    return 0 ;} 
posted on 2012-05-04 10:02  追逐.  阅读(282)  评论(0编辑  收藏  举报