POJ - 2234 Matches Game (尼姆博奕)

Dear 丶 2022-05-19 03:58 241阅读 0赞

Matches Game

Description

Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of matches, which is taken away, cannot be zero and cannot be larger than the number of matches in the chosen pile). If after a player’s turn, there is no match left, the player is the winner. Suppose that the two players are all very clear. Your job is to tell whether the player who plays first can win the game or not.

Input

The input consists of several lines, and in each line there is a test case. At the beginning of a line, there is an integer M (1 <= M <=20), which is the number of piles. Then comes M positive integers, which are not larger than 10000000. These M integers represent the number of matches in each pile.

Output

For each test case, output “Yes” in a single line, if the player who play first will win, otherwise output “No”.

Sample Input

2 45 45

3 3 6 9

Sample Output

No

Yes

题意描述:

尼姆博奕,有m堆石子且数量不一定,两人轮流取石子,可以从任意一堆取任意数量的石子(不能为0)先取完石子的人获胜;问先手是否能够取得胜利。

解题思路:
尼姆博弈将堆的石子全部按位异或若最后结果为0则先手输;否则先手赢。

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int sum,a[100],i,n;
  5. while(scanf("%d",&n)!=EOF)
  6. {
  7. sum=0;
  8. for(i=1;i<=n;i++)
  9. {
  10. scanf("%d",&a[i]);
  11. sum^=a[i];
  12. }
  13. if(sum==0)
  14. printf("No\n");
  15. else
  16. printf("Yes\n");
  17. }
  18. return 0;
  19. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ACM巴什

    NYOJ 题目23 取石子(一) ![20160805163317363][] 巴什博弈:只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后

    相关 博弈 HDU2176

    有三堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜。 这种情况最有意思,它与二进制有密切关系,我们用(a,b,c)表示某种局势,

    相关 博弈之巴什

    巴什博奕(Bash Game)是指这样的一个问题: 有一个含有n个物品的堆,两个人轮流从这堆物品中取物品, 规定每次至少取一个,最多取m个。最后取光者获胜。问你最后谁能获得胜