結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 14:06:30 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,999 bytes |
| コンパイル時間 | 332 ms |
| コンパイル使用メモリ | 82,908 KB |
| 実行使用メモリ | 84,056 KB |
| 最終ジャッジ日時 | 2025-06-12 14:06:38 |
| 合計ジャッジ時間 | 7,008 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 1 |
| other | AC * 2 TLE * 2 -- * 6 |
ソースコード
import sys
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(x):
if x < 2:
return False
if is_prime(x):
return True
max_e = x.bit_length()
for e in range(2, max_e + 1):
low = 2
high = x
while low <= high:
mid = (low + high) // 2
try:
m_power = mid ** e
except OverflowError:
m_power = x + 1 # Force it to be higher than x
if m_power == x:
if is_prime(mid):
return True
else:
break
elif m_power < x:
low = mid + 1
else:
high = mid - 1
return False
def main():
input = sys.stdin.read().split()
Q = int(input[0])
for i in range(1, Q + 1):
N = int(input[i])
if N % 2 == 0:
if N == 2:
print("No")
else:
print("Yes")
else:
found = False
a = 1
while a <= 60:
power = 1 << a # 2^a
if power >= N:
break
x = N - power
if x < 2:
a += 1
continue
if is_prime_power(x):
found = True
break
a += 1
print("Yes" if found else "No")
if __name__ == '__main__':
main()
gew1fw