POJ 2234 Matches Game(Nim博弈裸题)

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

Nim博弈裸题,所有堆的值异或得0则先手输,否则先手赢。

Nim博弈及异或理解可看百度百科

这游戏看上去有点复杂,先从简单情况开始研究吧。如果轮到你的时候,只剩下一堆石子,那么此时的必胜策略肯定是把这堆石子全部拿完一颗也不给对手剩,然后对手就输了。如果剩下两堆不相等的石子,必胜策略是通过取多的一堆的石子将两堆石子变得相等,以后如果对手在某一堆里拿若干颗,你就可以在另一堆中拿同样多的颗数,直至胜利。如果你面对的是两堆相等的石子,那么此时你是没有任何必胜策略的,反而对手可以遵循上面的策略保证必胜。如果是三堆石子……

如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<vector>
 5 #include<cstring>
 6 #include<string>
 7 #include<algorithm>
 8 using namespace std;
 9
10
11 int main()
12 {
13     int n;
14     long long tmp,res;
15     while(~scanf("%d",&n))
16     {
17         scanf("%lld",&res);
18         for(int i=1;i<n;i++)
19         {
20             scanf("%lld",&tmp);
21             res ^=tmp;
22         }
23         if(res==0)
24             printf("No\n");
25         else
26             printf("Yes\n");
27     }
28     return 0;
29 }
				
时间: 2024-12-17 21:56:37

POJ 2234 Matches Game(Nim博弈裸题)的相关文章

POJ 2451 nlog(n)半平面交裸题。

前言       最近学习C#,不过好在当初考计算机二级学习过C++,刚上手没有对C#感到很恐惧.C#视频也看了几天 了,总感觉不总结一下心里没底,现在跟着我从头走进C#之旅吧.     C#是以后总面向对象的编程语言(OOP),C#是从C和C++派生出来的,主要用于开发可以运行在.NET平台 上的应用程序.随着.NET的发展,C#语言简单.现代.面向对象和类型安全显示了一定的优势.     下面我就介绍一些初学者不太理解的一些东西.   C#有以下突出的特点       (1)语法简洁.不允许

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

【POJ2960】S-Nim SG函数 博弈 裸题模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42653601 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意: 两人轮流从若干堆石子中某堆取k个石子, k∈集合S, 就是每次取的数量被限定成某几个数的意思! 然后跟正常Nim一样谁不能操作就输. 题解: SG函数裸题. SG函数: 首先需要是有向无环图(拓扑图) 首先确定边界状态,SG值为0,然后暴力拓扑得出其它点的SG值. SG值为所有子集的SG值中未出现的最小自

POJ 2234 Matches Game 博弈论水题 Nim模型

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 mat

POJ 2234 Matches Game 尼姆博弈

题目大意:尼姆博弈,判断是否先手必胜. 题目思路: 尼姆博弈:有n堆各a[]个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 获胜规则:ans=(a[1]^a[2] --^a[n]),若ans==0则后手必胜,否则先手必胜. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h>

POJ 2234 Matches Game(取火柴博弈1)

传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int n; while(~scanf("%d",&n)) { int a,res=0; for(int i=0;i<n;i++) { scanf("%d",&a); res^

题解——POJ 2234 Matches Game

这道题也是一个博弈论 根据一个性质 对于\( Nim \)游戏,即双方可以任取石子的游戏,\( SG(x) = x \) 所以直接读入后异或起来输出就好了 代码 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int m; int main(){ while(scanf("%d",&m)!=EOF){ int ans=0,mid; f

取(m堆)石子游戏 HDU2176(Nim博弈)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题目: Problem Description m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎样取子.例如5堆 5,7,8,9,10先取者胜,先取者第1次取时可以从有8个的那一堆取走7个剩下1个,也可以从有9个的中那一堆取走9个剩下0个,也可以从有10个的中那一堆取走7个剩下3个. Input 输入有多组.每组第1行是m,m<=200000.

POJ 3164 Command Network 最小树形图-朱刘算法裸题

题目来源:POJ 3164 Command Network 题意:求以1为根的最小树形图 没有输出字符串 思路:直接高朱刘算法 不懂的可以百度 学会了就是直接套模板的事情 其实就是不断消圈而已 不构成圈就有解 无法从根到达其他点就无解 #include <cstdio> #include <cstring> #include <cmath> const int maxn = 110; const int maxm = 50010; const double INF =