您的位置:首页 > 其它

HDU/HDOJ1851 nim博弈和巴什博弈的结合

2013-07-16 22:09 357 查看
题目:题目链接

[align=left]Problem Description[/align]
Agrael likes play a simple game with his friend Animal during the classes. In this Game there are n piles of stones numbered from 1 to n, the 1st pile has M1 stones,
the 2nd pile has M2 stones, ... and the n-th pile contain Mn stones. Agrael and Animal take turns to move and in each move each of the players can take at most L1 stones from the 1st pile or take at most L2 stones
from the 2nd pile or ... or take Ln stones from the n-th pile. The player who takes the last stone wins.

After Agrael and Animal have played the game for months, the teacher finally got angry and decided to punish them. But when he knows the rule of the game, he is so interested in this game that he asks Agrael to play the game with him and if Agrael wins, he
won't be punished, can Agrael win the game if the teacher and Agrael both take the best move in their turn?

The teacher always moves first(-_-), and in each turn a player must takes at least 1 stones and they can't take stones from more than one piles.

 
[align=left]Input[/align]
The first line contains the number of test cases. Each test cases begin with the number n (n ≤ 10), represent there are n piles. Then there are n lines follows, the i-th line
contains two numbers Mi and Li (20 ≥ Mi > 0, 20 ≥ Li > 0).

 
[align=left]Output[/align]
Your program output one line per case, if Agrael can win the game print "Yes", else print "No".

 
[align=left]Sample Input[/align]

2
1
5 4
2
1 1
2 2

 
[align=left]Sample Output[/align]

Yes
No

题目的意思是:两个人玩游戏,老师和Ag,一开始是老师先玩的,游戏的规则是:这里有n堆石子,每一堆的石子数目是ai,每次当一个人拿的时候他可以选择拿一个到bi个,最后拿完石子的那个人获胜。

开始输入的是石子堆数,再者输入ai,bi。求如果Ag赢就输出“Yes”,否则“No”。

分析: 这道题目就是一个Nim和巴什博弈的结合,对每一堆石子来说,都是一个巴什博弈,这样的结果组合起来又是nim博弈。这样就很明显了。每一堆的结果就是一个sg值,这样就OK了。上代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
using namespace std;

int main()
{
int T;
int i,n;
int ans,m,l;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
ans=0;
for(i = 1; i <= n; i++)
{
scanf("%d%d",&m,&l);
ans = ans^(m % (l+1) );
}
if(ans==0)
printf("Yes\n");//后取的人胜
else
printf("No\n");//先取的人胜
}
return 0;
}


努力努力...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  巴什博弈 nim博弈