結果
問題 |
No.2724 Coprime Game 1
|
ユーザー |
![]() |
提出日時 | 2023-09-29 10:29:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 940 ms / 2,000 ms |
コード長 | 1,547 bytes |
コンパイル時間 | 188 ms |
コンパイル使用メモリ | 82,108 KB |
実行使用メモリ | 149,428 KB |
最終ジャッジ日時 | 2024-10-02 22:53:39 |
合計ジャッジ時間 | 7,435 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 7 |
ソースコード
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])