結果

問題 No.2724 Coprime Game 1
ユーザー MasKoaTSMasKoaTS
提出日時 2023-09-29 10:29:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 889 ms / 2,000 ms
コード長 1,547 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,608 KB
実行使用メモリ 149,680 KB
最終ジャッジ日時 2024-04-12 20:50:42
合計ジャッジ時間 7,152 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 825 ms
147,108 KB
testcase_01 AC 35 ms
54,152 KB
testcase_02 AC 53 ms
68,604 KB
testcase_03 AC 861 ms
149,068 KB
testcase_04 AC 884 ms
149,680 KB
testcase_05 AC 883 ms
149,232 KB
testcase_06 AC 878 ms
148,208 KB
testcase_07 AC 889 ms
148,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if(self.parents[x] < 0):
            return x
        self.parents[x] = self.find(self.parents[x])
        return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if(x == y):
            return
        if(self.parents[x] > self.parents[y]):
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parents[self.find(x)]

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

"""
Main Code
"""

query = [int(input()) for _ in [0] * int(input())]

n_max = max(query)
rad = [0] * (n_max + 1)
for i in range(2, int(n_max ** 0.5 + 2)):
    if(rad[i] != 0):
        continue
    for j in range(i * i, n_max + 1, i):
        rad[j] = i

uni = UnionFind(n_max + 1)
ans = [''] * (n_max + 1)
for n in range(2, n_max + 1):
    if(rad[n] == 0):
        ans[n] = 'P'
        continue
    x = n
    while(rad[x] != 0):
        r = rad[x]
        uni.unite(n, r)
        while(x % r == 0):
            x //= r
    if(x > 1):
        uni.unite(n, x)
    ans[n] = 'K' if((uni.size(n) - 1) & 1) else 'P'

for n in query:
    print(ans[n])
0