結果

問題 No.2724 Coprime Game 1
ユーザー MasKoaTSMasKoaTS
提出日時 2023-04-09 14:00:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 921 ms / 2,000 ms
コード長 1,299 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 149,536 KB
最終ジャッジ日時 2024-04-12 20:50:31
合計ジャッジ時間 7,245 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 852 ms
146,980 KB
testcase_01 AC 37 ms
53,524 KB
testcase_02 AC 57 ms
68,860 KB
testcase_03 AC 900 ms
149,296 KB
testcase_04 AC 912 ms
149,360 KB
testcase_05 AC 905 ms
149,536 KB
testcase_06 AC 903 ms
148,208 KB
testcase_07 AC 921 ms
149,232 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())]

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

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

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