Editorial for Isolation Game


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Editorialist: PHPeasant

Isolation Game is the first problem in the Unigames section of the 2020 Charity Unvigil Contest where you need to determine the winner in a very simple game. The sneaky trick is that a player the depletes the stack to 0 also looses.

The intended solution is to implement the game using Euclid's GCD algorithm. A verbose but explicit example implementation in C++ 14 is given below:

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

int gcd(int a , int b){
    int t;
    while(b!=0){
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b,n, curr;
    cin >> a >> b >> n;
    int winner = 0;
    while(true){
        //Taylor move
        curr = gcd(b, n);
        if(curr<n){
            n-=curr;
            winner = 1;
        } else {
            winner = 0;
            break;
        }
        if(n == 0){
            winner = 1;
            break;
        }
        //Gozz's move
        curr = gcd(a, n);
        if(curr < n){
            n-=curr;
            winner = 0;
        } else {
            winner = 1;
            break;
        }
        if(n == 0){
            winner = 0;
            break;
        }
    }
    cout << winner;
    return 0;
}

Comments

There are no comments at the moment.