結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-15 23:47:13 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,415 bytes |
| コンパイル時間 | 222 ms |
| コンパイル使用メモリ | 82,168 KB |
| 実行使用メモリ | 83,540 KB |
| 最終ジャッジ日時 | 2025-04-15 23:49:24 |
| 合計ジャッジ時間 | 6,726 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 1 |
| other | AC * 1 TLE * 1 -- * 8 |
ソースコード
import math
def is_prime(n):
if n <= 1:
return False
elif n <= 3:
return True
elif n % 2 == 0:
return False
d = n - 1
s = 0
while d % 2 == 0:
d //= 2
s += 1
bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
for a in bases:
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 is_prime_power(y):
if y < 2:
return False
if is_prime(y):
return True
max_a = y.bit_length()
for a in range(2, max_a + 1):
low = 2
high = y
found = False
while low <= high:
mid = (low + high) // 2
m = 1
try:
m = pow(mid, a)
except OverflowError:
high = mid - 1
continue
if m == y:
if is_prime(mid):
return True
else:
break
elif m < y:
low = mid + 1
else:
high = mid - 1
return False
small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
def solve():
import sys
input = sys.stdin.read().split()
Q = int(input[0])
cases = list(map(int, input[1:Q+1]))
for N in cases:
if N == 2:
print("No")
continue
# Check sum of two primes
if N % 2 == 0:
if is_prime(N - 2):
print("Yes")
continue
else:
if is_prime(N - 2):
print("Yes")
continue
# Check higher prime powers
found = False
for a in range(2, 61):
for p in small_primes:
x = p ** a
if x >= N:
break
rem = N - x
if rem < 2:
continue
if is_prime(rem) or is_prime_power(rem):
found = True
break
if found:
break
if found:
print("Yes")
continue
print("No")
if __name__ == "__main__":
solve()
lam6er