結果

問題 No.2 素因数ゲーム
ユーザー vjudge1
提出日時 2025-09-04 09:46:10
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,304 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 103,096 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-04 09:46:13
合計ジャッジ時間 2,657 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;

typedef long long ll;

vector<ll> primes;
map<ll, bool> memo[2];

void findPrimeFactors(ll n) {
    ll temp = n;
    for (ll i = 2; i * i <= temp; i++) {
        if (temp % i == 0) {
            primes.push_back(i);
            while (temp % i == 0) temp /= i;
        }
    }
    if (temp > 1) primes.push_back(temp);
}

bool canWin(int turn, ll num) {
    if (memo[turn].count(num)) return memo[turn][num];
    if (num == 1) return turn == 1;

    if (turn == 0) {
        for (ll p : primes) {
            if (num % p != 0) continue;
            ll nextNum = num;
            while (nextNum % p == 0) {
                nextNum /= p;
                if (canWin(1, nextNum)) return memo[turn][num] = true;
            }
        }
        return memo[turn][num] = false;
    } else {
        for (ll p : primes) {
            if (num % p != 0) continue;
            ll nextNum = num;
            while (nextNum % p == 0) {
                nextNum /= p;
                if (!canWin(0, nextNum)) return memo[turn][num] = false;
            }
        }
        return memo[turn][num] = true;
    }
}

int main() {
    ll n;
    cin >> n;
    findPrimeFactors(n);
    cout << (canWin(0, n) ? "Alice" : "Bob") << endl;
    return 0;
}
0