結果

問題 No.2724 Coprime Game 1
ユーザー rlangevinrlangevin
提出日時 2024-04-12 21:53:07
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,594 ms / 2,000 ms
コード長 1,730 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 124,416 KB
実行使用メモリ 222,340 KB
最終ジャッジ日時 2024-04-12 21:53:25
合計ジャッジ時間 15,418 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,437 ms
222,236 KB
testcase_01 AC 1,436 ms
222,172 KB
testcase_02 AC 1,437 ms
222,340 KB
testcase_03 AC 1,577 ms
222,180 KB
testcase_04 AC 1,594 ms
222,296 KB
testcase_05 AC 1,565 ms
222,160 KB
testcase_06 AC 1,504 ms
222,176 KB
testcase_07 AC 1,578 ms
222,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
private:
    vector<int> par;
    vector<int> rank;
    vector<int> size;

public:
    UnionFind(int n) {
        par.resize(n);
        rank.resize(n);
        size.resize(n);
        for (int i = 0; i < n; ++i) {
            par[i] = i;
            rank[i] = 0;
            size[i] = 1;
        }
    }

    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }

    void unionSet(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            if (rank[x] < rank[y]) {
                swap(x, y);
            }
            if (rank[x] == rank[y]) {
                rank[x]++;
            }
            par[y] = x;
            size[x] += size[y];
        }
    }

    bool isSame(int x, int y) {
        return find(x) == find(y);
    }

    int getSize(int x) {
        return size[find(x)];
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin >> T;

    const int M = 3 * 1000000 + 5;
    vector<int> L(M, 0);
    vector<vector<int>> G(M);
    for (int i = 2; i < M; ++i) {
        if (L[i]) {
            continue;
        }
        for (int j = 2 * i; j < M; j += i) {
            G[j].push_back(i);
            L[j] = 1;
        }
    }

    UnionFind uf(M);
    vector<int> ans(M, 0);
    for (int i = 2; i < M; ++i) {
        for (int j : G[i]) {
            uf.unionSet(i, j);
        }
        ans[i] = uf.getSize(i);
    }

    for (int _ = 0; _ < T; ++_) {
        int num;
        cin >> num;
        cout << (ans[num] % 2 == 0 ? "K" : "P") << endl;
    }

    return 0;
}
0