結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 14:00:52 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,978 bytes |
| コンパイル時間 | 409 ms |
| コンパイル使用メモリ | 82,360 KB |
| 実行使用メモリ | 130,340 KB |
| 最終ジャッジ日時 | 2025-06-12 14:01:54 |
| 合計ジャッジ時間 | 6,764 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 1 |
| other | TLE * 1 -- * 9 |
ソースコード
import sys
import math
def sieve(n):
sieve = [True] * (n + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(math.isqrt(n)) + 1):
if sieve[i]:
sieve[i*i : n+1 : i] = [False] * len(sieve[i*i : n+1 : i])
primes = [i for i, is_prime in enumerate(sieve) if is_prime]
return primes
primes = sieve(10**6)
def is_prime(n):
if n < 2:
return False
for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
if n % p == 0:
return n == p
d = n - 1
s = 0
while d % 2 == 0:
d //= 2
s += 1
for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]:
if a >= n:
continue
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(s - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
def kth_root(n, k):
if k == 0:
return None
low = 1
high = n
while low <= high:
mid = (low + high) // 2
try:
power = mid ** k
except OverflowError:
high = mid - 1
continue
if power == n:
return mid
elif power < n:
low = mid + 1
else:
high = mid - 1
return None
def is_prime_power(n):
if n < 2:
return False
if is_prime(n):
return True
max_k = n.bit_length()
for k in range(2, max_k + 1):
root = kth_root(n, k)
if root is not None and is_prime(root):
return True
return False
Q = int(sys.stdin.readline())
for _ in range(Q):
N = int(sys.stdin.readline())
if N % 2 == 0:
print("Yes" if N >= 4 else "No")
continue
# Check N-2
if N >= 3 and (N - 2) >= 2:
if is_prime_power(N - 2):
print("Yes")
continue
found = False
# Check small primes' exponents
for p in primes:
if p > N:
break
a = 1
while True:
try:
x = p ** a
except OverflowError:
break
if x > N:
break
y = N - x
if y < 2:
break
if is_prime_power(y):
found = True
break
a += 1
if found:
break
if found:
print("Yes")
continue
# Check small primes' higher exponents for y, x = N - y
for q in primes:
if q > N:
break
b = 2
while True:
try:
y = q ** b
except OverflowError:
break
if y > N:
break
x = N - y
if x < 2:
break
if is_prime(x):
found = True
break
b += 1
if found:
break
print("Yes" if found else "No")
gew1fw