POJ Matches Game

目录

1.题目

2.中文翻译

3.思路解析

        3.1 题目来源

        3.2 异或操作

4.代码(python)


1.题目

Matches Game

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 14934Accepted: 8570

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

2.中文翻译

题目描述

        这里有一个简单的游戏。在这场比赛中,有几堆火柴和两名选手。两个人轮流上场。在每一轮中,人们可以选择一个桩,并从该桩中拿走任意数量的匹配(当然,被拿走的匹配数量不能为零,也不能大于所选火柴堆中的数量)。如果在一个玩家回合后,没有剩下的火柴,该玩家就是赢家。假设两位选手都很清楚。你的工作是判断先上场的球员是否能赢得比赛。

输入

        输入由几行组成,每行中都有一个测试用例。在一条线的开头,有一个整数M(1<=M<=20),这是火柴堆的数量。然后是M个正整数,它们不大于10000000。这M个整数表示每个堆中的火柴数。

输出

        对于每个测试用例,如果先玩的玩家获胜,则在一行中输出“是”,否则输出“否”。

样例输入

2 45 45
3 3 6 9

样例输出

No
Yes

3.思路解析

        3.1 题目来源

        这道题可以使用博弈论的思想来解决。对于每一堆火柴棍,我们将其视为一个独立的游戏,用 Nim 游戏来分析。

        在 Nim 游戏中,假设当前有 n 个石子,两个玩家轮流操作,每次可以取走任意多个石子,但不能不取。那么无论双方如何操作,最后剩下一个石子的玩家获胜。

        Nim 游戏有一个重要的特性:当且仅当所有堆的石子数量的异或和等于 0 时,先手必败。否则,先手必胜。

        基于以上思路,我们可以将每一堆火柴棍的数量进行异或运算,然后判断异或结果是否为 0。如果是 0,则先手必败,输出 "No";如果不是 0,则先手必胜,输出 "Yes"。

        3.2 异或操作

         在Python中,异或操作使用符号"^"表示。异或运算是一种逻辑运算符,用于比较两个操

  作数的每一位。它的规则如下:

  • 如果两个操作数的对应位相同,则结果为0。
  • 如果两个操作数的对应位不同,则结果为1。
  • 当一个数与0进行异或操作时,结果将保持不变。(适用于任何进制)

4.代码(python)

while True:
    result=0
    #控制输入 n表示火柴堆的数量 seq存储每一个火柴堆
    n,*seq=map(int,input().split())
    
    #在Python中,0与任何数进行异或操作的结果仍然是那个数本身
    for i in seq:
        result^=i
    
    #根据题意控制输出
    if result==0:
        print("No")
    else:
        print("Yes")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值