hdu 1907 John

取火柴的游戏

题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根,

可将一堆全取走,但不可不取,最后取完者为胜,求必胜的方法。

若sum=0;则先取者输,后取者胜;

若sum=!0,则先取者使其变成奇异状态,先取者胜

即谁先面临奇异状态谁输;

题目2:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根,

可将一堆全取走,但不可不取,最后取完者为负,求必胜的方法。

分为2种情况:1:每个堆数的火柴数量为1,若为奇数堆,先取者最后取完,则输;

若为偶数堆,后取则输

2、每个堆数的火柴数量不是1,若sum=0;则先取者输,后取者胜,

若sum=!0,先取者胜,后取者输

总结:谁先面临奇异状态谁输,不管哪种情况!

代码如下:

#include<stdio.h>
int main()
{
  int T,a[100],i,sum,m,ok;
  while(~scanf("%d",&T))
  {
    while(T--)
     {
            sum=0;
            ok=0;
           scanf("%d",&m);
        for(i=1;i<=m;i++)
          {
             scanf("%d",&a[i]);
             if(a[i]>1) ok=1;
             sum=sum^a[i];
          }
          if(ok==0)
           {
                if(m%2==0)
                  printf("John\n");
                  else
                   printf("Brother\n");
           }
           else
            if(sum==0)
              printf("Brother\n");
              else
               printf("John\n");
     }
  }
  return 0;
}

hdu 1907 John

时间: 2025-01-01 15:42:50

hdu 1907 John的相关文章

POJ 3480 &amp; HDU 1907 John(尼姆博弈变形)

题目链接: PKU:http://poj.org/problem?id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Description Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to e

HDU 1907 John(博弈)

题目 参考了博客:http://blog.csdn.net/akof1314/article/details/4447709 //0 1 -2 //1 1 -1 //0 2 -1 //1 2 -1 //2 2 -2 //0 3 -1 //1 3 -1 //2 3 -1 //3 3 -2 //0 4 -1 //1 4 -1 //2 4 -1 //3 4 -1 //4 4 -2 //0 5 -1 //1 5 -1 //2 5 -1 //3 5 -1 /* 尼姆博弈.对于N堆的糖,一种情况下是每堆都是

hdu 1907 John(anti nim)

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4704    Accepted Submission(s): 2720 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1907(Nim博弈)

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4407    Accepted Submission(s): 2520 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

HDU 1907、2907 【博弈、anti-NIM】

NIM游戏 和 anti-NIM游戏: NIM游戏,是取走最后一个石子者胜 而anti-NIM游戏,是取走最后一个石子者败 游戏通用准则: 1. 若一个局面为必胜态,则总存在一种操作方式将当前状态转化为必败态 2. 若一个局面为必败态,则所有操作都只能转化为必胜态 NIM游戏的解法: 将当前局面的所有堆的石子数进行异或(也称为:NIM sum), 1)若NIM sum=0,则当前局面为必败态 2)若NIM sum≠0,则当前局面为必胜态 #include <cstdio> #include &

HDU 1907

传送门 规则类似与NIM博弈,但是取到最后一颗石子的人输 必败态有两种 异或和不为0且(石子数多余1的堆)数目为0 异或和为0且(石子数多余1的堆)数目大于1 1 #include <queue> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 #define INF

hdu 2509 Be the Winner

详解:hdu 1907 John - lihaogegehuting的专栏 - 博客频道 - CSDN.NET 两道题几乎一样 代码如下: #include<stdio.h> int main() { int T,a[100],i,sum,ok; while(~scanf("%d",&T)) { sum=0; ok=0; for(i=1;i<=T;i++) { scanf("%d",&a[i]); if(a[i]>1) ok=