結果

問題 No.2724 Coprime Game 1
ユーザー MasKoaTS
提出日時 2024-01-03 21:28:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 2,006 ms
コンパイル使用メモリ 198,424 KB
最終ジャッジ日時 2025-02-18 16:03:17
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

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


int main(){
    int t;    cin >> t;
    vector<int> query(t);

    int n_max = 0;
    for(auto& n : query){
        cin >> n;
        if(n_max < n){
            n_max = n;
        }
    }

    vector<bool> is_prime(n_max + 1, true);
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i * i <= n_max; ++i){
        if(!is_prime[i]){
            continue;
        }
        for(int j = i * i; j <= n_max; j += i){
            is_prime[j] = false;
        }
    }

    fenwick_tree<int> bt(n_max + 1);
    for(int i = 2; i < n_max + 1; ++i){
        bt.add(i, is_prime[i]);
    }
    for(auto& n : query){
        if(is_prime[n]){
            cout << 'P' << endl;
            continue;
        }
        int cnt = n - 2 - bt.sum(n / 2 + 1, n);
        if(cnt & 1){
            cout << 'K' << endl;
        }
        else{
            cout << 'P' << endl;
        }
    }

    return 0;
}
0