結果
問題 |
No.577 Prime Powerful Numbers
|
ユーザー |
![]() |
提出日時 | 2025-06-12 14:00:52 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,849 bytes |
コンパイル時間 | 288 ms |
コンパイル使用メモリ | 82,904 KB |
実行使用メモリ | 140,276 KB |
最終ジャッジ日時 | 2025-06-12 14:01:58 |
合計ジャッジ時間 | 6,740 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 1 |
other | TLE * 1 -- * 9 |
ソースコード
import math 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(n): if is_prime(n): return True max_b = int(math.log2(n)) + 1 for b in range(2, max_b + 1): low = 1 high = n found = False while low <= high: mid = (low + high) // 2 power = mid ** b if power == n: found = True break elif power < n: low = mid + 1 else: high = mid - 1 if found and is_prime(mid): return True return False def generate_high_powers(N): candidates = [] p = 2 max_p = int(math.isqrt(N)) + 1 while p <= max_p: if is_prime(p): a = 2 current = p * p while current <= N: candidates.append(current) a += 1 next_val = p ** a if next_val > N or next_val < current: break current = next_val p += 1 if p > 10**6: break return candidates 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, val in enumerate(sieve) if val] return primes def generate_low_primes(N): limit = 10**6 primes = sieve(limit) return [p for p in primes if p <= N] def generate_near_primes(N): candidates = [] max_qb = 10**6 qb_list = set() primes = sieve(int(math.isqrt(max_qb)) + 1) for q in primes: if q < 2: continue a = 2 while True: q_power = q ** a if q_power > max_qb: break qb_list.add(q_power) a += 1 for q in primes: if q <= max_qb: qb_list.add(q) for qb in qb_list: x = N - qb if x >= 2 and is_prime(x): candidates.append(x) for delta in [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]: x = N - delta if x >= 2 and is_prime(x): candidates.append(x) return candidates def solve(): Q = int(sys.stdin.readline()) for _ in range(Q): N = int(sys.stdin.readline()) if N < 2: print("No") continue high_powers = generate_high_powers(N) low_primes = generate_low_primes(N) near_primes = generate_near_primes(N) candidates = set(high_powers + low_primes + near_primes) found = False for x in candidates: if x > N: continue y = N - x if y < 1: continue if is_prime_power(y): found = True break if not found: for x in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]: if x > N: continue y = N - x if y < 1: continue if is_prime_power(y): found = True break if found: print("Yes") else: print("No") if __name__ == "__main__": solve()