【POJ】2234 Matches Game(博弈论)

http://poj.org/problem?id=2234

博弈论真是博大精深orz

首先我们仔细分析很容易分析出来,当只有一堆的时候,先手必胜;两堆并且相同的时候,先手必败,反之必胜。

根据博弈论的知识(论文 张一飞:《由感性认识到理性认识——透析一类搏弈游戏的解答过程》)

局面可以分解,且结果可以合并。

局面均是先手

当子局面是 胜 和 败,那么局面则为胜

当子局面是 败 和 胜,那么局面则为胜

当子局面是 败 和 败,那么局面则为败

当子局面为 胜 和 胜,那么局面为不确定

而这些性质一一对应二进制的异或运算。

我们设局面表示为S,败的局面就表示为#S=0,胜的局面就表示为#S!=0

设二进制a和b

当a!=0 && b==0时, a^b!=0

当a==0 && b!=0时,b^a!=0

当a==0 && b==0时,a^b=0

当a!=0 && b!=0时,a^b可能=0也可能!=0

而又设函数f(x)=#x #x表示为x的二进制

那么就可以根据上边的运算,合并局面成最终局面

还是看论文吧,,我也不熟,今晚上还要仔细地研究,太博大精深了。orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=30;
int n;

int main() {
	int ans;
	while(~scanf("%d", &n)) {
		ans=0;
		rep(i, n) ans^=getint();
		ans?(puts("Yes")):(puts("No"));
	}
	return 0;
}


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

Source

POJ Monthly,readchild

时间: 2025-01-09 21:59:11

【POJ】2234 Matches Game(博弈论)的相关文章

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(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

这道题也是一个博弈论 根据一个性质 对于\( 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

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 尼姆博弈

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

POJ【数论/组合/博弈论】

 POJ[数论/组合/博弈论]题目列表 POJ[数论/组合/博弈论]题目列表 原来的列表比较水,今天换了一个难一些的列表,重新开始做~ 红色的代表已经AC过,蓝色的代表做了但是还没过.这句话貌似在我空间里的每份列表里都有额. 博弈论 POJ 2234 Matches Game POJ 2975 Nim POJ 2505 A multiplication game POJ 1067 取石子游戏 POJ 2484 A Funny Game POJ 2425 A Chess Game POJ 29

POJ 2348-Euclid&#39;s Game(博弈论)

Euclid's Game Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2348 Appoint description:  System Crawler  (2015-03-11) Description Two players, Stan and Ollie, play, starting with two natural num

POJ 2234

1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #define MAXN 100 5 using namespace std; 6 //把所有堆的石子数目用二进制数表示出来,当全部这些数按位异或结果为0时当前局面为必败局面,否则为必胜局面: 7 int a[MAXN]; 8 void op(int & num); 9 int trans_10_to_2(int num); 10 i

Part.4【博弈论】

---恢复内容开始--- 不要问我为什么突然跳到Part.4,我懒得解释. 在蔡大神的论文+讲解和HZW的题库下,自己大概是明白什么是博弈论的皮毛了吧. 先说SG定理吧. 对于游戏中的状态,我们给每个状态定义一个必胜态和必败态.区别在于前者可以通过一次操作到达必败态,但后者无法做到(后者在一次操作后所能到达的状态全部都为必胜态) 接着引进SG函数,每个状态都有一个SG值,这个值由它所能到达的状态的SG值决定.(这里的所能到达的状态指的是经过一次操作能到达的状态,下同) SG值有以下性质: SG值