GG and MM
GG and MM like playing a game since they are children. At the beginning of game, there are two piles of stones. MM chooses a pile of stones first, which has x stones, and then she can choose a positive number k and remove k*x stones out from the other pile of stones, which has y stones (I think all of you know that y>=k*x - -!). Then it comes the turn of GG, followed the rules above-mentioned as well. When someone can't remove any stone, then he/she loses the game, and this game is finished.
Many years later, GG and MM find this game is too simple, so they decided to play N games at one time for fun. MM plays first, as the same, and the one on his/her turn must play every unfinished game. Rules to remove are as same as above, and if someone cannot remove any stone (i.e., loses the last ending game), then he/she loses. Of course we can assume GG and MM are clever enough, and GG will not lose intentionally, O(∩_∩)O~
Many years later, GG and MM find this game is too simple, so they decided to play N games at one time for fun. MM plays first, as the same, and the one on his/her turn must play every unfinished game. Rules to remove are as same as above, and if someone cannot remove any stone (i.e., loses the last ending game), then he/she loses. Of course we can assume GG and MM are clever enough, and GG will not lose intentionally, O(∩_∩)O~
The first line of each test case is an integer N, N<=1000, which represents there are N games, then N lines following, each line has two numbers: p and q, standing for the number of the two piles of stones of each game, p, q<=1000(it seems that they are so leisure = =!), which represent the numbers of two piles of stones of every game.
The input will end with EOF.
3 1 1 1 1 1 1 1 3 2
MM GG
题意:这道题有n个游戏同时进行,每个游戏都是两堆石子,每次操作从大的那一堆中取走小的那一堆的数量的倍数,最后取不了的人输。
思路:这种同时进行n个游戏的博弈是典型的Every-SG,在这种博弈里,整个游戏的胜负取决于最后一场子游戏,所以可以赢的局一定要尽可能延长游戏时间,会输的局一定要让游戏尽快结束。在这篇文章里有解决Every-SG问题的教学。
http://blog.csdn.net/kele52he/article/details/77009784
#include<cstdio>
int cnt=0;
bool dfs(int a,int b)
{
if(a<b) a^=b,b^=a,a^=b;
if(b==0) return 0;
if(!dfs(b,a%b)){cnt++;return 1;}
if(a/b>1){cnt+=2;return 1;}
else {cnt++;return 0;}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int x,y,a=0,b=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
cnt=0;
if(dfs(x,y)) a=a>cnt?a:cnt;
else b=b>cnt?b:cnt;
}
puts(a>b?"MM":"GG");
}
return 0;
}