GG and MM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 273 Accepted Submission(s): 108
Problem Description
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~
Input
The input file contains multiply test cases (no more than 100).
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.
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.
Output
For each test case, output the name of the winner.
Sample Input
3 1 1 1 1 1 1 1 3 2
Sample Output
MM GG
Author
alpc95
Source
Recommend
zhengfeng
这是个every-sg问题.
做过一个简单的不是every-sg,而是只有一组数,用大的那个数减去小的那个数的整数倍,谁先减到0,谁就赢了。
那个的必胜策略是,谁先到达n/m>1的状态,谁就有必胜策略。
而这个游戏,由于有多组,所以对于有必胜策略的那组,要延长游戏的步数,而对于没有必胜策略的那组,就要缩短游戏的时间,然后看谁的必胜策略的那组数的时间长,谁
就是整场游戏的必胜者。
代码是参考一个大神的~
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<iostream>
#define M 1002
using namespace std;
int n,sg[M][M],step[M][M];
int find(int a,int b)
{
if(sg[a][b]>=0) return sg[a][b];
if(a>b) swap(a,b);
int mi=99999999,ma=0;
for(int i=a;i<=b;i+=a)
{
if(find(a,b-i)==0)//后一步是必败点
{
ma=max(ma,step[a][b-i]);//当前点是必胜点,所以要延时
sg[a][b]=sg[b][a]=1;
}
else mi=min(mi,step[a][b-i]);
}
if(sg[a][b]==1)
{
step[a][b]=step[b][a]=ma+1;
return 1;
}
step[a][b]=step[b][a]=mi+1;
return sg[a][b]=sg[b][a]=0;
}
int main(){
memset(sg,-1,sizeof(sg));
for(int i=0;i<=1000;i++) step[0][i]=step[i][0]=sg[i][0]=sg[0][i]=0;
while(~scanf("%d",&n)){
int ma=0,ma1=0,a,b;
while(n--)
{
scanf("%d %d",&a,&b);
if(a==0||b==0) continue;
if(find(a,b)==1)
{
if(step[a][b]>ma)
ma=step[a][b];
}
else
{
if(step[a][b]>ma1)
ma1=step[a][b];
}
}
puts(ma>ma1?"MM":"GG");
}
return 0;
}