結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 21:18:46 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,069 bytes |
| コンパイル時間 | 177 ms |
| コンパイル使用メモリ | 82,116 KB |
| 実行使用メモリ | 104,688 KB |
| 最終ジャッジ日時 | 2025-06-12 21:19:07 |
| 合計ジャッジ時間 | 6,545 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 1 |
| other | TLE * 1 -- * 9 |
ソースコード
import sys
import math
def sieve(limit):
sieve = [True] * (limit + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(math.sqrt(limit)) + 1):
if sieve[i]:
for j in range(i*i, limit+1, i):
sieve[j] = False
primes = [i for i, is_prime in enumerate(sieve) if is_prime]
return primes
# Precompute primes up to 1e6
primes_list = sieve(10**6)
def is_prime(n):
if n < 2:
return False
for p in primes_list:
if p * p > n:
break
if n % p == 0:
return False
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 integer_nth_root(n, b):
if n == 0:
return 0
low = 1
high = n
while low <= high:
mid = (low + high) // 2
powered = 1
overflow = False
for _ in range(b):
powered *= mid
if powered > n:
overflow = True
break
if overflow:
high = mid - 1
elif powered == n:
return mid
elif powered < n:
low = mid + 1
else:
high = mid - 1
powered = 1
for _ in range(b):
powered *= high
if powered > n:
break
if powered == n:
return high
else:
return -1
def is_prime_power(n):
if n < 2:
return False
for b in range(1, 61):
x = integer_nth_root(n, b)
if x == -1:
continue
if x ** b == n and is_prime(x):
return True
return False
def main():
input = sys.stdin.read().split()
Q = int(input[0])
for i in range(1, Q+1):
N = int(input[i])
found = False
# Handle a=1 case: p is a prime, rem = N - p must be a prime
# Alternatively, rem = N - q^b must be a prime
# So, loop through all possible q^b and check if rem is prime
for b in range(1, 61):
for q in primes_list:
q_power = q ** b
if q_power > N - 2:
break
rem = N - q_power
if rem < 2:
continue
if is_prime(rem):
print("Yes")
found = True
break
if found:
break
if found:
continue
# Handle a >=2 case: p^a is part, rem must be a prime power
for a in range(2, 61):
if a > 60:
break
max_p = int((N - 2) ** (1.0 / a))
for p in primes_list:
if p > max_p:
break
p_power = p ** a
rem = N - p_power
if rem < 2:
continue
if is_prime_power(rem):
print("Yes")
found = True
break
if found:
break
if found:
continue
# Check the other way around for a=1 (when p is a prime and rem is q^b)
# This part is similar to the a=1 case but we have to do it again as the previous loop may not cover all possibilities
for b in range(1, 61):
for q in primes_list:
q_power = q ** b
if q_power > N - 2:
break
rem = N - q_power
if rem < 2:
continue
if is_prime_power(rem):
print("Yes")
found = True
break
if found:
break
if found:
continue
print("No")
if __name__ == "__main__":
main()
gew1fw