hdu 1525 Euclid's Game(博弈 找規律)

Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):

25 7
11 7
4 7
4 3
1 3
1 0

an Stan wins.

Input
The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.

Output
For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.

Sample Input

34 12
15 24
0 0

Sample Output

Stan wins
Ollie wins

題解:
題目:不停的用較大的數減去兩者中較小的數的倍數,直到一方是另一方的倍數爲止,誰先得到x, nx的形式誰就贏

假設輸入得兩個數爲n 和 m, 假定 n>m
- 1.若n % m == 0 由題意知,先手一定贏
- 2.若n % m != 0時
- 2.1 當n > 2m, 由於先手很聰明,所以他知道(n % m, m)的局面是否爲必勝態。如果爲必勝態,先手就將(n, m)的局面變成(n % m + b, b), 讓後手不得不爲先手創造出(n %m, m)的必勝態, 先手贏;如果爲必敗態,先手就將(n, m)的局面變成(n % m, m), 這樣必敗態的下一個局面還是必勝態, 先手贏。
結論:n > 2m 時, 先手必勝
- 2.2 當m < n < 2m 他的下一步一定是(n - m, m),先手沒有選擇的餘地, 所以遇到這種情況 只能聽天由命,不停相減 直到遇到n% m == 0的情況停止,這就不一定先手能贏嘍~讓程序說話~

os:我沒注意到是可以減倍數這一點,,,所以遲遲找不到規律,,,太菜了

 #include<bits/stdc++.h>
 using namespace std;

int main(){
    int n, m;
    while(scanf("%d%d", &n, &m), n||m){
        bool flag = 1;
        if(n < m)
            swap(n, m);
        while(m){
            if(n % m == 0 || n > 2*m){
                break;
            }
            else{
                n = n-m;
                swap(n, m);
                flag ^= 1;//異或同假異真 判斷先手是否能位於必勝點
            }
        }
        if(flag)
             printf("Stan wins\n");
        else if(flag == 0)
            printf("Ollie wins\n");
    }
    return 0;
}
發佈了196 篇原創文章 · 獲贊 316 · 訪問量 200萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章