結果

問題 No.577 Prime Powerful Numbers
ユーザー lam6er
提出日時 2025-04-15 23:41:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,934 bytes
コンパイル時間 1,617 ms
コンパイル使用メモリ 82,092 KB
実行使用メモリ 115,800 KB
最終ジャッジ日時 2025-04-15 23:44:05
合計ジャッジ時間 8,701 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other TLE * 2 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

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(y):
    if y < 2:
        return False
    if is_prime(y):
        return True
    max_b = int(math.log2(y)) + 1 if y > 1 else 2
    for b in range(2, max_b + 1):
        m = round(y ** (1.0 / b))
        if m < 2:
            continue
        if pow(m, b) == y:
            if is_prime(m):
                return True
        elif pow(m + 1, b) == y:
            if is_prime(m + 1):
                return True
    return False

def sieve(max_p):
    if max_p < 2:
        return []
    sieve_list = [True] * (max_p + 1)
    sieve_list[0] = sieve_list[1] = False
    for i in range(2, int(math.isqrt(max_p)) + 1):
        if sieve_list[i]:
            sieve_list[i*i : max_p + 1 : i] = [False] * len(sieve_list[i*i : max_p + 1 : i])
    primes = [i for i, is_p in enumerate(sieve_list) if is_p]
    return primes

def solve():
    input = sys.stdin.read().split()
    Q = int(input[0])
    cases = list(map(int, input[1:Q+1]))
    for N in cases:
        found = False
        # Step 1: Check sum of two primes (exponents 1 and 1)
        if N % 2 == 0:
            if N == 2:
                pass
            else:
                if is_prime(N - 2):
                    print("Yes")
                    continue
        else:
            if N >= 3 and is_prime(N - 2):
                print("Yes")
                continue
        # Step 2: Check a >=3
        max_a = 60
        for a in range(3, max_a + 1):
            max_p = int(N ** (1.0 / a)) + 1
            primes = sieve(max_p)
            for p in primes:
                x = pow(p, a)
                if x > N:
                    continue
                y = N - x
                if y < 2:
                    continue
                if is_prime_power(y):
                    found = True
                    break
            if found:
                break
        if found:
            print("Yes")
            continue
        # Step 3: Check a=2
        primes_a2 = sieve(10**6)
        for p in primes_a2:
            x = p * p
            if x > N:
                break
            y = N - x
            if y < 2:
                continue
            if is_prime_power(y):
                found = True
                break
        if found:
            print("Yes")
            continue
        # If all checks fail
        print("No")

solve()
0