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 kx stones out from the other pile of stones, which has y stones (I think all of you know that y>=kx - -!). 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


Translate:
GG和MM從小就喜歡玩遊戲。遊戲開始時,有兩堆石頭。MM首先選擇一堆石頭,其中有x個石頭,然後她可以選擇一個正數k,從另一堆石頭中取出kx個石頭,其中有y個石頭(我想你們都知道y>=kx–!)。接下來是GG的輪換,也遵循了上述規則。當有人不能移除任何石頭時,他/她將輸掉遊戲,遊戲結束。

很多年後,GG和MM發現這個遊戲太簡單了,所以他們決定一次玩N個遊戲。MM先玩,同樣,輪到他/她玩的必須是每一個未完成的遊戲。移除規則與上述相同,如果有人無法移除任何石頭(即,輸掉最後一場比賽),則他/她將輸掉。當然我們可以假設GG和MM足夠聰明,GG不會故意輸,O(∩∩∩)~
輸入
輸入文件包含多個測試用例(不超過100個)。
每個測試用例的第一行是一個整數N,N<=1000,表示有N個遊戲,接下來的N行,每行有兩個數字:p和q,代表每個遊戲的兩堆石頭的數量,p,q<=1000(看起來他們是如此的休閒=!),表示每場比賽的兩堆石頭的數量。
輸入將以EOF結束。
輸出
對於每個測試用例,輸出獲勝者的名稱。
Sample Input
3
1 1
1 1
1 1
1
3 2
Sample Output
MM
GG


大體思路:
這個就像下棋一樣,最後一步是制勝的關鍵,所以我們看誰能走到最後一步,走的多的人自然贏的機率大。寫一個函數,求步數,誰的最大步數最多,誰就贏了(具體細節請看代碼)
代碼:

#include <bits/stdc++.h>
using namespace std;
int sum;
int f(int p,int q){
	if(p<q)
		swap(p,q);//確保p比q大
	if(!q)	return 0;	//說明正在搬石頭的人已經輸了
	if(!f(q,p%q)){	//如果下一個人沒法辦了,說明這個人贏了(g或m)
		sum++;
		return 1;
	}
	if(p/q>1){	//不留或留q個,使局面對我更有利
		sum+=2;
		return 1;
	}	
	sum++;
	return 0;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF){
		int g=0,m=0;
		for(int i=0;i<n;i++){
			int p,q;
			scanf("%d%d",&p,&q);
			sum=0;
			if(f(p,q))
				g=max(g,sum);
			else	m=max(m,sum);
		}
		if(g>m)	printf("MM\n");
		else	printf("GG\n");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章