Luogu P1290 Euclid games / UVA10368 Euclid's Game

Luogu P1290 Euclid games / UVA10368 Euclid's Game

For the subject of game theory no contact number, and this road is more classic SG game, so we can only push themselves to the relationship ......
Suppose we have two numbers \ (m, the n-\) , we first \ ( m \) is set to a large value, \ (n-\) is set to a small value. Now it is divided into three cases:
1. If the number is a multiple of two relations, one winning current operation.
2. If \ (m \ n-div>. 1 \) , then the operation is the winning party.
why?
Take \ ((25,7) \) for example. This time, the operating side, there are three options: \ ((8,7) \) , \ ((11,7) \) , \ ((4,7) \) ,
if he chose \ ((18,7) \) , and that the latter would face a \ ((11,7) \) or \ ((4,7) \) ; and if he does not choose \ ((18,7) \) , then he faced or \ ((11,7) \) or \ ((4,7) \) .
At this time we will find, \ ((11,7) \) and \ ((4,7) \)Is a must win, and the two will choose the best policy, so who has the right to choose who will win, that he can not choose \ ((18,7) \) .
So easy to know, who has the right to choose who will win.
3. The quotient is \ (1 \) , continue to
give an example, such as \ ((6,4) \) , then the upper hand no choice, it will only continue, such as \ ((2,4) \) .
To sum up, then it is easy to write the code.
(Read the two questions are slightly different)

Luogu 1290

#include<bits/stdc++.h>

using namespace std;

int c,m,n;

bool Judge(int m,int n) {
    int p=0;
    while(1) {
        p++;
        int mmax=max(m,n),mmin=min(m,n);
        m=mmax;
        n=mmin;
        if(m%n==0||m/n>1) {
            return p%2;
        }
        else {
            m-=n;
        }
    }
}
int main()
{
    scanf("%d",&c);
    for(int i=1;i<=c;i++) {
        scanf("%d%d",&m,&n);
        if(Judge(m,n)==1) {
            printf("Stan wins\n");
        }
        else {
            printf("Ollie wins\n");
        }
    }
    return 0;
}

UVA10368

#include<bits/stdc++.h>

using namespace std;

int c,m,n;

bool Judge(int m,int n) {
    int p=0;
    while(1) {
        p++;
        int mmax=max(m,n),mmin=min(m,n);
        m=mmax;
        n=mmin;
        if(m%n==0||m/n>1) {
            return p%2;
        }
        else {
            m-=n;
        }
    }
}
int main()
{
    while(scanf("%d%d",&m,&n)&&(m!=0&&n!=0)) {
        if(Judge(m,n)==1) {
            printf("Stan wins\n");
        }
        else {
            printf("Ollie wins\n");
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/luoshui-tianyi/p/11449572.html