結果

問題 No.577 Prime Powerful Numbers
ユーザー gew1fw
提出日時 2025-06-12 21:18:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,954 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 90,112 KB
最終ジャッジ日時 2025-06-12 21:18:46
合計ジャッジ時間 6,569 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other TLE * 1 -- * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import random
import math

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 == 1:
        return False
    if is_prime(x):
        return True
    spf = None
    if x % 2 == 0:
        spf = 2
    else:
        i = 3
        max_i = int(math.isqrt(x)) + 1
        while i <= max_i:
            if x % i == 0:
                spf = i
                break
            i += 2
        if spf is None:
            return True  # x is a prime, but is_prime(x) should have handled it
    while x % spf == 0:
        x = x // spf
    return x == 1

def main():
    input = sys.stdin.read().split()
    Q = int(input[0])
    cases = list(map(int, input[1:Q+1]))
    for N in cases:
        found = False
        
        # Check for a in 2 to 60
        for a in range(2, 61):
            max_p = int(N ** (1.0 / a)) + 1
            p = 2
            while p <= max_p:
                if is_prime(p):
                    x = p ** a
                    if x > N:
                        break
                    y = N - x
                    if y < 2:
                        p += 1
                        continue
                    if is_prime_power(y):
                        print("Yes")
                        found = True
                        break
                p += 1
            if found:
                break
        
        if found:
            continue
        
        # Check for a=1, b >=2
        for b in range(2, 61):
            max_q = int((N - 2) ** (1.0 / b)) + 1
            q = 2
            while q <= max_q:
                if is_prime(q):
                    y = q ** b
                    if y >= N:
                        break
                    x = N - y
                    if is_prime(x):
                        print("Yes")
                        found = True
                        break
                q += 1
            if found:
                break
        
        if found:
            continue
        
        # Check for a=1, b=1 (sum of two primes)
        # Handle even numbers
        if N >= 4 and N % 2 == 0:
            print("Yes")
            found = True
        else:
            # Check if N is odd and N-2 is prime
            if N - 2 >= 2 and is_prime(N - 2):
                print("Yes")
                found = True
        
        if not found:
            print("No")

if __name__ == "__main__":
    main()
0