結果

問題 No.2724 Coprime Game 1
ユーザー 👑 eoeoeoeo
提出日時 2024-04-22 22:05:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 8,237 bytes
コンパイル時間 3,531 ms
コンパイル使用メモリ 260,136 KB
実行使用メモリ 16,764 KB
最終ジャッジ日時 2024-10-14 21:53:37
合計ジャッジ時間 5,911 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
16,756 KB
testcase_01 AC 101 ms
16,632 KB
testcase_02 AC 101 ms
16,764 KB
testcase_03 AC 237 ms
16,756 KB
testcase_04 AC 293 ms
16,756 KB
testcase_05 AC 289 ms
16,756 KB
testcase_06 AC 196 ms
16,760 KB
testcase_07 AC 286 ms
16,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef MY_DEBUG
#define dbg(var) debug_out(#var, "=", var)
#define dbgm(...) debug_out(__VA_ARGS__)
#else
#define dbg(...)
#define dbgm(...)
#endif
void debug_out();
template <class Head, class... Tail> void debug_out(Head, Tail...);
template <class T> bool chmax(T&, const T&);
template <class T> bool chmin(T&, const T&);
template <class T> T pwr(T, ll);
template <class T> T squ(T x) { return x * x; }
ll fact(ll);
ll comb(ll, ll);
ll ctoll(char);
char lltoc(ll);
bool flg(ll b, ll i) { return ((b) >> (i)) & 1LL; }  // flg(0b010, 1LL) -> true
const ll LINF = (ll)4e18 + 7;
const double EPS = 1e-9;
const int MAX_DUBUG_SIZE = 10;

unordered_set<int> prime_set(const int N) {
    vector<bool> is_prime(N + 1);
    for (int i = 0; i <= N; i++) {
        is_prime[i] = true;
    }
    unordered_set<int> P;
    for (int i = 2; i <= N; i++) {
        if (is_prime[i]) {
            for (int j = 2 * i; j <= N; j += i) {
                is_prime[j] = false;
            }
            P.insert(i);
        }
    }
    return P;
}

// エラトステネスの篩による素数列挙
// 計算量:O(N log log N)
vector<ll> prime_vector(ll N) {
    vector<bool> is_prime(N + 1, true);
    is_prime[0] = is_prime[1] = false;
    vector<ll> res;
    for (ll i = 2; i <= N; i++) {
        if (is_prime[i]) {
            for (ll j = 2 * i; j <= N; j += i) {
                is_prime[j] = false;
            }
            res.push_back(i);
        }
    }
    for (auto&& x : res) {
        x *= 2;
    }
    return res;
}
/*
    使い方

    vector<ll> pv = prime_vector(100);
    // pv = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... }
*/

auto ps = prime_set(3000000);
auto pv = prime_vector(3000000);

void solve([[maybe_unused]] int test) {
    ll n;
    cin >> n;
    dbg(pv);

    if (ps.contains(n)) {
        cout << 'P' << endl;
    }
    else {
        auto itr1 = upper_bound(pv.begin(), pv.end(), n);  // set, mapは map.lower_bound(探したい数字)
        ll idx1 = itr1 - pv.begin();  // x <= a[idx] となる最小のidx, 存在しない場合は idx = ssize(a)
        auto itr2 = lower_bound(pv.begin(), pv.end(), 2 * n);  // set, mapは map.lower_bound(探したい数字)
        ll idx2 = itr2 - pv.begin();  // x <= a[idx] となる最小のidx, 存在しない場合は idx = ssize(a)

        ll ans = (n - 2) - (idx2 - idx1);

        dbg(test);
        dbg(n);
        dbg(ans);
        dbg(idx1);
        dbg(idx2);
        dbg('\n');

        if (ans % 2 == 0) {
            cout << 'P' << endl;
        }
        else {
            cout << 'K' << endl;
        }
    }
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    cout << fixed << setprecision(12), cerr << fixed << setprecision(12);

    int testcase_size = 1;
    cin >> testcase_size;

    for (int _t = 0; _t < testcase_size; _t++) {
        solve(_t);
    }
}

/*



























-----------------------------------MY_FUNCTIONS------------------------------------ */

template <class First, class Second> ostream& operator<<(ostream& os, const pair<First, Second>& pp);
template <class T> ostream& operator<<(ostream& os, const vector<T>& V);
template <class T> ostream& operator<<(ostream& os, const vector<vector<T>>& VV);
template <class T> ostream& operator<<(ostream& os, const vector<vector<vector<T>>>& VVV);
template <class T> ostream& operator<<(ostream& os, const set<T>& SS);
template <class Key, class Tp> ostream& operator<<(ostream& os, const map<Key, Tp>& MM);
void debug_out();
void debug_out_vl(vector<ll> V);
void debug_out_vvl(vector<vector<ll>> VV);
template <class Head, class... Tail> void debug_out(Head H, Tail... T);

template <class First, class Second> ostream& operator<<(ostream& os, const pair<First, Second>& pp) {
    return os << "{" << pp.first << "," << pp.second << "}";
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& V) {
    if (V.empty()) return os << "[]";
    os << "[";
    for (ll i = 0; i < (ll)V.size(); i++) {
        os << V[i] << (i == int(V.size() - 1) ? "]" : ",");
    }
    return os;
}

template <class T> ostream& operator<<(ostream& os, const vector<vector<T>>& VV) {
    if (VV.empty()) return os << "[[]]";
    os << "[" << endl;
    for (auto&& V : VV) {
        os << V << endl;
    }
    os << "]";
    return os;
}

template <class T> ostream& operator<<(ostream& os, const vector<vector<vector<T>>>& VVV) {
    if (VVV.empty()) return os << "[[[]]]";
    os << "[" << endl;
    int cnt = 0;
    for (auto&& VV : VVV) {
        os << cnt++ << VV << endl << endl;
    }
    os << "]";
    return os;
}

template <class T> ostream& operator<<(ostream& os, const set<T>& SS) {
    if (SS.empty()) return os << "[]";
    os << "[";
    auto ii = SS.begin();
    for (; ii != SS.end(); ii++) os << *ii << (ii == prev(SS.end()) ? "]" : ",");
    return os;
}

template <class Key, class Tp> ostream& operator<<(ostream& os, const map<Key, Tp>& MM) {
    if (MM.empty()) return os << "[{:}]";
    os << "[";
    auto ii = MM.begin();
    for (; ii != MM.end(); ii++)
        os << "{" << ii->first << ":" << ii->second << "}" << (ii == prev(MM.end()) ? "]" : ",");
    return os;
}

void debug_out() { cerr << endl; }

void debug_out_vl(vector<ll> V) {
    const int MAX_SIZE = min((int)V.size(), MAX_DUBUG_SIZE);

    cerr << "\033[33m";
    if (V.empty()) {
        cerr << "[]" << endl;
        return;
    }
    cerr << "[";
    for (int i = 0; i < MAX_SIZE; i++) {
        if (V[i] == LINF)
            cerr << "INF";
        else
            cerr << V[i];

        if (i == (int)V.size() - 1)
            cerr << "]" << endl;
        else if (i == MAX_DUBUG_SIZE - 1)
            cerr << ",..." << endl;
        else
            cerr << ",";
    }
    return;
}

void debug_out_vvl(vector<vector<ll>> VV) {
    cerr << "\033[33m";
    if (VV.empty()) {
        cerr << "[[]]" << endl;
        return;
    }
    cerr << "[" << endl;

    int MAX_ROW = min((int)VV.size(), MAX_DUBUG_SIZE);
    for (int i = 0; i < MAX_ROW; i++) {
        const int MAX_COLUMN = min((int)VV[i].size(), MAX_DUBUG_SIZE);
        if (VV[i].empty()) {
            cerr << "[]" << endl;
            continue;
        }
        cerr << "[";
        for (int j = 0; j < MAX_COLUMN; j++) {
            if (VV[i][j] == LINF)
                cerr << "INF";
            else
                cerr << VV[i][j];

            if (j == (int)VV[i].size() - 1)
                cerr << "]" << endl;
            else if (j == MAX_DUBUG_SIZE - 1)
                cerr << ",..." << endl;
            else
                cerr << ",";
        }

        if (i != (int)VV.size() - 1 and i == MAX_DUBUG_SIZE - 1) {
            cerr << ":\n:\033[m" << endl;
            return;
        }
    }

    cerr << "]\033[m" << endl;
    return;
}

template <class Head, class... Tail> void debug_out(Head H, Tail... T) {
    if constexpr (std::is_same_v<Head, vector<ll>>) {
        debug_out_vl(H);
    }
    else if constexpr (std::is_same_v<Head, vector<vector<ll>>>) {
        debug_out_vvl(H);
    }
    else {
        cerr << "\033[33m" << H << "\033[m ";
    }

    debug_out(T...);
}

template <class T> bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template <class T> T pwr(T x, ll n) {
    T res = 1;
    for (int i = 0; i < n; i++) {
        res *= x;
    }
    return res;
}

ll fact(ll n) {
    ll res = 1;
    for (ll i = 1; i <= n; i++) res *= i;
    return res;
}

ll comb(ll n, ll r) {  // comb(60, 30)までオーバーフローなし
    ll res = 1;
    for (int i = 1; i <= r; i++) {
        res *= n--;
        res /= i;
    }
    return res;
}

ll ctoll(char c) {
    if (c < '0' or '9' < c) {
        cerr << "\n\033[33m ctoll に '0'~'9' 以外の文字が入りました.\033[m\n" << endl;
    }
    return ll(c - '0');
}

char lltoc(ll n) {
    if (n < 0 or 9 < n) {
        cerr << "\n\033[33m lltoc に 0 ~ 9 以外の数字が入りました.\033[m\n" << endl;
    }
    return char(n + '0');
}
0