HDU-1907 John(尼姆博弈)

题干:

给你n堆糖果,每堆糖果颜色不同,一次只能吃一种颜色且至少吃一个,每堆有ai个,吃掉最后一个糖果的人失败。John先手。John赢输出John,否则输出Brother。

思路:

从n堆里面拿东西,每次至少拿一个,先拿完者输,是一个典型的尼姆博弈。
除了全为一的情况判断n的奇偶;其他情况按位异或看看能否必胜。
放上kuangbin大佬的尼姆博弈的解释

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
int x;
int main()
{
	int n,t;
	scanf("%d",&t);
	while(t--)
	{
		int sum=0,ans=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			if(x==1)  sum++;
			ans=ans^x;
		}
		if(sum==n)
		{
			if(n&1)
				printf("Brother\n");
			else
				printf("John\n");
		}
		else
		{
			if(ans)
				printf("John\n");
			else
				printf("Brother\n");
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42279796/article/details/88527711
今日推荐