0
点赞
收藏
分享

微信扫一扫

hdu1907 John--尼姆博弈


原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1907​​

分析:
注意此题是最后取光的是loser。

现在有两个人分别是:先手–pre和后手–nex;

假设现在有n堆,每堆都有若干元素。

这些堆有的只有1个元素,有的堆的元素大于1个,现在把只有一个元素的堆放在左边,元素个数大于1的堆放在右边。

在两人博弈的时候,元素个数大于1的堆,最终,我说的是最终,无外乎变为0或者1,变为1的就放在左边;变为0的直接消失了,不管它。

那么最后一定会出现这样的局面:左边有m堆,m>=0,每个堆的元素个数都是1;右边只有一个堆,元素个数大于1。

现在一个人来取,那么根据m的奇偶性来决定右边的这个堆的剩余个数是0还是1。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<string>
#include<stdio.h>
#define INF 1000000000
#define EPS 1e-6
using namespace std;

int t;
int n;
int a[50];

int main()
{
scanf("%d", &t);
while (t--)
{
bool flag = 0;
int ans = 0;

scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
ans ^= a[i];
if (a[i] != 1)
flag = 1;
}

if (flag)
{
if (ans == 0)
printf("Brother\n");
else
printf("John\n");
}
else
{
if (n % 2 == 0)
printf("John\n");
else
printf("Brother\n");
}
}
return 0;
}


举报

相关推荐

0 条评论