hdu 3595 GG and MM (Every-SG游戏)

GG and MM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 687    Accepted Submission(s): 303


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~
 

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.
 

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

题解:Every-SG游戏

这个游戏就是分成若干不相干的组,每次操作能移动的都要移动。这个游戏的特点不仅有必胜和必败,而且有时间长短的博弈。对于自己必胜的局面,希望步数越多越好,自己必败的局面,早点结束才有利。

必胜当且仅当所有的单一游戏步数最大的为奇数。最大的为奇数,当然是先手赢,其他的都已经提前结束。

其实判断的时候也可以比较一下必胜态与必败态那个持续的时间更长。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define N 2003
using namespace std;
int sg[N][N],step[N][N];
int n,m;
int get(int x,int y)
{
	if (sg[x][y]!=-1) return sg[x][y];
    if (x>y) swap(x,y);
    int mx=0,mn=1000000000;
    for (int i=x;i<=y;i+=x) {
    	if (!get(x,y-i)) {
    		sg[x][y]=sg[y][x]=1;
    		mx=max(mx,step[x][y-i]);
		}
		else mn=min(mn,step[x][y-i]);
	}
	//cout<<x<<" "<<y<<" "<<mx<<" "<<mn<<" "<<sg[x][y]<<endl;
	if (sg[x][y]==1){
		step[x][y]=step[y][x]=mx+1;
		return sg[x][y];
	}
	step[x][y]=step[y][x]=mn+1;
	sg[x][y]=sg[y][x]=0;
	return sg[x][y];
}
int main()
{
	freopen("a.in","r",stdin);
	memset(sg,-1,sizeof(sg));
	for (int i=0;i<=1000;i++)
	 sg[i][0]=sg[0][i]=step[i][0]=step[0][i]=0;
	while (scanf("%d",&n)!=EOF) {
		int a=0,b=0;
		for (int i=1;i<=n;i++) {
			int x,y; scanf("%d%d",&x,&y);
			get(x,y);
			if (x==0||y==0) continue;
			if (sg[x][y]) a=max(a,step[x][y]);
			else b=max(b,step[x][y]);
		}
		if (a>b) puts("MM");
		else puts("GG");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值