結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー lam6er
提出日時 2025-04-16 01:13:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,224 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 82,016 KB
実行使用メモリ 144,092 KB
最終ジャッジ日時 2025-04-16 01:14:50
合計ジャッジ時間 12,788 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other AC * 1 WA * 10 TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

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

sieve_primes = sieve(10**6)

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 factorize(x, primes):
    factors = {}
    for p in primes:
        if p * p > x:
            break
        while x % p == 0:
            factors[p] = factors.get(p, 0) + 1
            x //= p
    if x > 1:
        if is_prime(x):
            factors[x] = 1
        else:
            i = 2
            while i * i <= x:
                while x % i == 0:
                    factors[i] = factors.get(i, 0) + 1
                    x //= i
                i += 1
            if x > 1:
                factors[x] = 1
    return factors

def find_min_p(S, primes):
    for p in primes:
        if p not in S:
            return p
    candidate = 2
    while True:
        if candidate not in S and is_prime(candidate):
            return candidate
        candidate += 1

def main():
    input = sys.stdin.read().split()
    T = int(input[0])
    cases = list(map(int, input[1:T+1]))
    for X in cases:
        if X == 1:
            print(2)
            continue
        factors = factorize(X, sieve_primes)
        S = set(factors.keys())
        p = find_min_p(S, sieve_primes)
        Y1 = X * p
        if factors:
            min_y2 = min(p**(a+1) for p, a in factors.items())
            Y2 = X * min_y2
            print(min(Y1, Y2))
        else:
            print(Y1)

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