結果

問題 No.1243 約数加算
ユーザー gew1fw
提出日時 2025-06-12 15:22:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 2,788 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,768 KB
実行使用メモリ 79,156 KB
最終ジャッジ日時 2025-06-12 15:22:05
合計ジャッジ時間 4,242 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import random

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 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 = math.gcd(abs(x - y), n)
        if d != n:
            return d

def factor(n):
    factors = []
    def _factor(n):
        if n == 1:
            return
        if is_prime(n):
            factors.append(n)
            return
        d = pollards_rho(n)
        _factor(d)
        _factor(n // d)
    _factor(n)
    factors.sort()
    return factors

def generate_divisors(n):
    if n == 0:
        return []
    factors = factor(n)
    factor_counts = {}
    for p in factors:
        if p in factor_counts:
            factor_counts[p] += 1
        else:
            factor_counts[p] = 1
    divisors = [1]
    for p, cnt in factor_counts.items():
        temp = []
        for d in divisors:
            current = d
            for e in range(1, cnt + 1):
                current *= p
                temp.append(current)
        divisors += temp
    divisors = list(set(divisors))
    divisors.sort(reverse=True)
    return divisors

def main():
    input = sys.stdin.read().split()
    idx = 0
    T = int(input[idx])
    idx += 1
    for _ in range(T):
        A = int(input[idx])
        B = int(input[idx+1])
        idx += 2
        if A == B:
            print(0)
            print()
            continue
        current = B
        steps = []
        while current != A:
            max_possible = current - A
            divisors = generate_divisors(current)
            found = False
            for d in divisors:
                if d <= max_possible:
                    steps.append(d)
                    current -= d
                    found = True
                    break
            if not found:
                print("No solution found")
                return
        steps = steps[::-1]
        K = len(steps)
        print(K)
        print(' '.join(map(str, steps)))

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