結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー gew1fw
提出日時 2025-06-12 21:32:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,491 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 99,176 KB
最終ジャッジ日時 2025-06-12 21:33:58
合計ジャッジ時間 23,384 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other AC * 1 WA * 10 TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import random
from math import gcd

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, 325, 9375, 28178, 450775, 9780504, 1795265022]:
        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 pollards_rho(n):
    if n % 2 == 0:
        return 2
    if n % 3 == 0:
        return 3
    if n % 5 == 0:
        return 5
    while True:
        c = random.randint(1, n-1)
        f = lambda x: (pow(x, 2, n) + c) % n
        x, y, d = 2, 2, 1
        while d == 1:
            x = f(x)
            y = f(f(y))
            d = gcd(abs(x - y), n)
        if d != n:
            return d

def factorize(n):
    factors = {}
    def _factor(n):
        if n == 1:
            return
        if is_prime(n):
            factors[n] = factors.get(n, 0) + 1
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    return factors

def find_min_prime(factors_set):
    primes_list = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293]
    for p in primes_list:
        if p not in factors_set:
            return p
    p = 2
    while True:
        if p not in factors_set:
            return p
        p += 1

def main():
    input = sys.stdin.read().split()
    T = int(input[0])
    idx = 1
    for _ in range(T):
        X = int(input[idx])
        idx += 1
        if X == 1:
            print(2)
            continue
        factors = factorize(X)
        primes_set = set(factors.keys())
        D = 1
        for p in factors:
            D *= (factors[p] + 1)
        if 2 * D != 2 * D:
            print(0)
            continue
        p = find_min_prime(primes_set)
        Y_p = X * p
        Y_list = [Y_p]
        for p_j in factors:
            a_j = factors[p_j]
            Y_j = X * (p_j ** (a_j + 1))
            Y_list.append(Y_j)
        min_Y = min(Y_list)
        print(min_Y)

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