結果

問題 No.577 Prime Powerful Numbers
ユーザー lam6er
提出日時 2025-04-15 23:41:36
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,410 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 81,416 KB
実行使用メモリ 144,544 KB
最終ジャッジ日時 2025-04-15 23:43:51
合計ジャッジ時間 6,537 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other TLE * 1 -- * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

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, is_p in enumerate(sieve) if is_p]
    return primes

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
    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(m):
    if m < 2:
        return False
    if is_prime(m):
        return True
    max_b = m.bit_length()
    for b in range(max_b, 1, -1):
        low = 2
        high = int(m ** (1 / b)) + 2
        while low <= high:
            mid = (low + high) // 2
            power = 1
            overflow = False
            for _ in range(b):
                power *= mid
                if power > m:
                    overflow = True
                    break
            if overflow:
                high = mid - 1
                continue
            if power < m:
                low = mid + 1
            elif power > m:
                high = mid - 1
            else:
                if is_prime(mid):
                    return True
                else:
                    break
    return False

primes_1e5 = sieve(10**5)
small_primes = sieve(10**6)

prime_powers_1e6 = set()
for p in small_primes:
    a = 1
    current = p
    while current <= 1e6:
        prime_powers_1e6.add(current)
        a += 1
        current = p ** a
        if current > 1e6:
            break

def solve():
    import sys
    input = sys.stdin.read().split()
    Q = int(input[0])
    cases = list(map(int, input[1:Q+1]))
    for N in cases:
        found = False
        a = 1
        while True:
            x = 2 ** a
            if x >= N:
                break
            y = N - x
            if y < 2:
                a += 1
                continue
            if is_prime_power(y):
                found = True
                break
            a += 1
        if found:
            print("Yes")
            continue
        for p in primes_1e5:
            if p == 2:
                continue
            a = 1
            current = p
            while current <= N:
                x = current
                y = N - x
                if y < 2:
                    break
                if is_prime_power(y):
                    found = True
                    break
                a += 1
                current *= p
                if current > N:
                    break
            if found:
                break
        if found:
            print("Yes")
            continue
        for y in prime_powers_1e6:
            if y >= N:
                continue
            x = N - y
            if x < 2:
                continue
            if is_prime_power(x):
                found = True
                break
        if found:
            print("Yes")
            continue
        print("No")

solve()
0