"Game Theory" Euclid's Game

Problem A. Euclid's game

Time limit 1000 ms
Memory limit 128 MB

Title description

  Two descendants of Euclid, Stan and Ollie, are playing a number game, which was invented by their ancestor Euclid. Given two positive integers M and N, starting from Stan, subtract the positive integer multiple of the smaller number from the larger one. Of course, the number obtained cannot be less than 0. Then Ollie did the same operation on the number he just got, and the smaller number of M and N... Until one person got 0, he won. Here is how they used the two numbers (
25, 7 ) to play the game: Start: 25 7
Stan: 11 7
Ollie: 4 7
Stan: 4 3
Ollie: 1 3
Stan: 1 0
Stan won the game.
Now, assuming they operate perfectly, who will win?

Input data

The first line is the group number CC of the test data. There are CC rows below, each row contains a set of data, containing two positive integers M,NM,N. (M, N (M, N does not exceed the long integer.)

Output Data

Output one line for each set of input data, if Stan wins, output "Stan wins"; otherwise output "Ollie wins"

Sample input

2
25 7
24 15

Sample output

Stan wins
Ollie wins

Ideas:

Analysis: Because every time is an optimal operation, obviously when a person can have two choices, obviously, if odd multiples do not win, even multiples must win, that is, when maxNum/minNum>=2, that person must win,
when When a person has only one choice, that is, when m/n=1, he can only operate, but he cannot be sure of winning or losing. At this time, he can judge when maxNum%minNum=0 eg, 7/1 then his opponent must lose. That he must win 

AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<math.h>

using namespace std;

void output(bool flag)
{
    if(flag)
        cout<<"Stan wins"<<endl;
    else
        cout<<"Ollie wins"<<endl;
}

//解析:因为每次都是最优化操作,显然当一个人可以有两种选择时,显然,如果奇数倍不赢,偶数倍必赢 即当maxNum/minNum>=2时,那个人必赢,
//当一个人只有一次选择时,即m/n=1时,他只能操作,但并不能确定输赢,这时候可以判断什么时候maxNum%minNum=0 即 7/1 那么他的对手必输。即他必赢
bool solve(long long maxNum,long long minNum,long long time)
{
    if(maxNum%minNum==0||maxNum/minNum>=2)
        return time&1? false :true;//time odd ollie win 偶数 stan win
    return solve(minNum,maxNum%minNum,time+1);
}

int main()
{
    ios::sync_with_stdio(false);
    int c;
    cin>>c;
    long long m,n;
    while(c--)
    {
        cin>>m>>n;
        output(solve(max(m,n),min(m,n),0));
    }

    return 0;
}

 

Guess you like

Origin blog.csdn.net/Look_star/article/details/109345972