結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-15 23:47:32 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,278 bytes |
| コンパイル時間 | 273 ms |
| コンパイル使用メモリ | 81,456 KB |
| 実行使用メモリ | 130,016 KB |
| 最終ジャッジ日時 | 2025-04-15 23:49:47 |
| 合計ジャッジ時間 | 6,518 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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.sqrt(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 is_prime_power(x):
if x < 2:
return False
if is_prime(x):
return True
max_b = min(60, int(math.log2(x)) + 1)
for b in range(2, max_b + 1):
low = 2
high = x
found = False
while low <= high:
mid = (low + high) // 2
m = mid ** b
if m == x:
if is_prime(mid):
return True
else:
found = True
break
elif m < x:
low = mid + 1
else:
high = mid - 1
if found:
continue
return False
def solve():
input = sys.stdin.read().split()
Q = int(input[0])
for i in range(1, Q + 1):
N = int(input[i])
found = False
for p in primes:
if p > N:
break
s = p
while True:
x = N - s
if x < 2:
break
if is_prime_power(x):
found = True
break
next_s = s * p
if next_s > N or next_s < s: # overflow check
break
s = next_s
if found:
break
print("Yes" if found else "No")
if __name__ == '__main__':
solve()
lam6er