Stone Division – HackerRank

Đề bài

https://www.hackerrank.com/challenges/stone-division/problem

Hướng dẫn

Code

C++
#include <iostream>
#include <map>
#define ll long long
using namespace std;

const ll MAX_M = 10;

ll s[MAX_M];
ll n, m;
map<ll, bool> memo;

bool check(ll x)
{
    if (memo.count(x))
    {
        return memo[x];
    }

    for (ll i = 0; i < m; i++)
    {
        if (x % s[i] == 0 && s[i] % 2 == 0)
        {
            return memo[x] = true;
        }
    }

    for (ll i = 0; i < m; i++)
    {
        if (x % s[i] == 0 && check(x / s[i]) == false)
        {
            return memo[x] = true;
        }
    }

    return memo[x] = false;
}

int main()
{
    cin >> n >> m;

    for (ll i = 0; i < m; i++)
    {
        cin >> s[i];
    }

    if (check(n))
    {
        cout << "First" << endl;
    }
    else
    {
        cout << "Second" << endl;
    }
}