結果
問題 |
No.577 Prime Powerful Numbers
|
ユーザー |
![]() |
提出日時 | 2025-03-20 18:49:56 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,394 bytes |
コンパイル時間 | 142 ms |
コンパイル使用メモリ | 82,768 KB |
実行使用メモリ | 129,108 KB |
最終ジャッジ日時 | 2025-03-20 18:52:25 |
合計ジャッジ時間 | 6,524 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 1 |
other | TLE * 1 -- * 9 |
ソースコード
import math def sieve(limit): sieve = [True] * (limit + 1) sieve[0] = sieve[1] = False for i in range(2, int(math.isqrt(limit)) + 1): if sieve[i]: sieve[i*i : limit+1 : i] = [False] * len(sieve[i*i : limit+1 : i]) primes = [i for i, val in enumerate(sieve) if val] return primes primes = sieve(10**6) def is_prime(n): if n < 2: 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 nth_root(n, b): if n == 0: return 0 low = 1 high = n while low <= high: mid = (low + high) // 2 try: powered = mid ** b except OverflowError: powered = float('inf') if powered == n: return mid elif powered < n: low = mid + 1 else: high = mid - 1 return None def is_prime_power(n): if n < 2: return False max_exp = n.bit_length() for b in range(max_exp, 0, -1): root = nth_root(n, b) if root is None: continue try: if root ** b == n and is_prime(root): return True except OverflowError: continue return False Q = int(input()) for _ in range(Q): N = int(input()) if N < 4: print("No") continue found = False max_a = 1 while (2 ** (max_a + 1)) <= (N - 2): max_a += 1 max_a = min(max_a, 60) for a in range(1, max_a + 1): x = 2 ** a if x > N - 2: break y = N - x if y < 2: continue if is_prime_power(y): print("Yes") found = True break if found: continue if (N - 2) >= 2: y = N - 2 if is_prime_power(y): print("Yes") continue if (N - 3) >= 2: y = N - 3 if is_prime_power(y): print("Yes") continue for p in primes: if p > N - 2: break y = N - p if y < 2: continue if is_prime_power(y): print("Yes") found = True break if found: continue for p in primes: x = p ** 2 if x > N - 2: break y = N - x if y < 2: continue if is_prime_power(y): print("Yes") found = True break if found: continue for exponent in range(3, 61): max_p = int((N - 2) ** (1.0 / exponent)) if max_p < 2: continue for p in primes: if p > max_p: break x = p ** exponent if x > N - 2: continue y = N - x if y < 2: continue if is_prime_power(y): print("Yes") found = True break if found: break if found: continue print("No")