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;
}
努力努力...
[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;
}
努力努力...
相关文章推荐
- HDOJ2188(基本的巴什博弈,套用公式即可)
- 20140715 「博弈 - Nim(尼姆)博弈」 HDOJ 1907 John
- hdoj 2188 悼念512汶川大地震遇难同胞——选拔志愿者 【博弈论&&巴什博弈】
- HDOJ 题目1566&题目1544 S-Nim(sg博弈模板)
- hdu 1851 A Simple Game (Nim博弈)
- HDOJ---1846 Brave Game[巴什博弈]
- HDOJ 1850 Being a Good Boy in Spring Festival(Nim博弈)
- HDOJ 1536 S-Nim 博弈 SG函数
- HDOJ 2188 悼念512汶川大地震遇难同胞——选拔志愿者 【巴什博弈】
- HDOJ 1847 Good Luck in CET-4 Everybody!(巴什博弈)
- hdoj 1847 Good Luck in CET-4 Everybody! (巴什博弈)
- HDOJ---1847 Good Luck in CET-4 Everybody![巴什博弈]
- HDOJ Brave Game(巴什博弈)
- HDOJ 1907 John (nim博弈)
- hdoj Be the Winner 2509 (NIM博弈)
- 巴什博弈 威佐夫博弈 nim博弈 斐波那契博弈
- 巴什博弈+威佐夫博弈+Nim博弈
- HDOJ 1730 Northcott Game(nim博弈)
- HDOJ 3032 Nim or not Nim? (博弈 :SG)
- hdoj1847(巴什博弈)