1. 程式人生 > >博弈——GG and MM

博弈——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~ 
InputThe 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. 
OutputFor 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

題意:

每組給n個遊戲,每個遊戲有兩堆石頭,GG和MM輪流操作,操作規則:

從兩堆裡面選出一堆,假設這堆石頭有x個,然後在另一堆裡取k*x個石頭(k是正整數)

誰不能取石頭誰輸,MM先手。

思路:

這是一個every——sg遊戲。

決定總遊戲勝負的是最後一局遊戲的勝負。因為不能取石頭的情況就已經是最後一局了,所以之前的遊戲勝負情況沒有意義。

那麼為了自己能贏,對於自己會贏的遊戲,我肯定想盡可能地延長時間,對於自己會輸的遊戲,我肯定想盡可能地結束。

那麼可以找出每一局所走的時間,最後進行判斷即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int n;
int cnt;
int 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)    //如果a/b>1,那麼勝負也決定在我手中
    {            //因為對於a%b這局面只可能是贏或輸,我就可以對應地不留或留b個,使局面對我有利
        cnt+=2;
        return 1;
    }
    cnt++;
    return 0;
}
int main()
{
    while(~scanf("%d", &n))
    {
        int a=0,b=0;   //a表示MM能贏的遊戲中步數最大值,b是GG能贏的最大步數
        for(int i=0; i<n; i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            cnt=0;
            if(dfs(x, y))
                a=max(cnt, a);
            else
                b=max(b, cnt);
        }
        printf("%s\n", a>b?"MM":"GG");
    }
    return 0;
}


相關推薦

博弈——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 st

GG and MM HDU

題解:Every_SG 2,Every-SG 定義: 1,Every-SG 遊戲規定,對於還沒有結束的單一遊戲,遊戲者必須對該遊 戲進行一步決策,也就是每輪操作要對所有單一遊戲進行操作。

About SD and MM Accounting Entries

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Java網路程式設計-你是GG還是MM?

第六階段 網路程式設計 每一臺計算機通過網路連線起來,達到了資料互動的效果,而網路程式設計所解決的問題就是如何讓程式與程式之間實

博弈sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

paths 相同 friend pri 產生 chan star ren have Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered

UVa 12525 Boxes and Stones (dp 博弈)

uoj case empty eps ted lin stand lld ber Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered fr

HDU 1848(sg博弈) Fibonacci again and again

ace main esp 數量 mode oid else while n) Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K

HDU 1848 Fibonacci again and again(簡單博弈SG函數)

sg函數 pro htm break www amp 函數 true .cn 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 題目: 1、 這是一個二人遊戲;2、 一共有3堆石子,數量分別是m, n, p個;3、

POJ 1704 Georgia and Bob(階梯博弈)

suppose 4.0 endif game contains edge div rip -i Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11357

POJ 1704 Georgia and Bob(階梯博弈)題解

n-2 有一個 n-1 seed class ++ article scanf clas 題意:有一個一維棋盤,有格子標號1,2,3,......有n個棋子放在一些格子上,兩人博弈,只能將棋子向左移,不能和其他棋子重疊,也不能跨越其他棋子,不能超越邊界,不能走的人輸 思路:

CodeForces - 347C - Alice and Bob(博弈)

CodeForces - 347C - Alice and Bob It is so boring in the summer holiday, isn’t it? So Alice and Bob have invented a new game to play. The rules

【AtCoder2376】Black and White Tree(博弈

題意 A和B輪流給樹上的結點染色,A每次選擇沒染過的點染成白色,B每次選擇沒染過的點染成黑色,最後若所有白色都與黑色相鄰,則B勝,否則A勝。雙方以最優策略,求A勝還是B勝。 題解 A首先選擇葉子結點的父

hdu Hakase and Nano (博弈)

Problem C. Hakase and Nano Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs of pebbles, and the i-th

Light OJ-1344 Aladdin and the Game of Bracelets DP(記憶化搜尋) + SG函式 博弈

題目描述 It’s said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we

913. Cat and Mouse (圖上博弈+ dp )

A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: graph[a]&n

【HDU 3590】 PP and QQ (博弈-Anti-SG遊戲,SJ定理,樹上刪邊遊戲)

PP and QQ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 510    Accepted Submission(s): 256 Probl

1704 Georgia and Bob (Nim博弈的變形)

題目: Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2

博弈之 Nim 遊戲&poj 3537 Crosses and Crosses

Nim遊戲的定義 Nim遊戲是組合遊戲(Combinatorial Games)的一種,準確來說,屬於“Impartial Combinatorial Games”(以下簡稱ICG)。滿足以下條件的遊戲是ICG(可能不太嚴謹): 有兩名選手

用Wireshark抓包來揭開ftp client GG和ftp server MM的勾搭內容並用C程式碼來簡要模擬實現Windows自帶的ftp client

      前面, 我們玩過http, 頗有點意思, 在本文中, 我們繼續來玩ftp(file transfer protocol).   http和ftp都是建立在tcp之上的應用層協議, 無論他們怎麼包裝, 怎麼裝bigger, 最終還是基於tcp端到端傳輸的。本文主要

wyh2000 and pupil

cor other miss 貪心 -i mission spa size foo wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Other