結果

問題 No.2365 Present of good number
ユーザー lam6er
提出日時 2025-03-20 20:29:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,809 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 56,100 KB
最終ジャッジ日時 2025-03-20 20:29:55
合計ジャッジ時間 2,860 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

MOD = 10**9 + 7

def factorize(n):
    factors = defaultdict(int)
    while n % 2 == 0:
        factors[2] += 1
        n //= 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] += 1
            n //= i
        i += 2
    if n > 1:
        factors[n] += 1
    return factors.items()

def main():
    N, K = map(int, sys.stdin.readline().split())
    if K == 0:
        print(N % MOD)
        return
    current_factors = dict(factorize(N))
    steps_done = 0
    has_other = True  # Assume initial check required

    while steps_done < K and has_other:
        new_factors = defaultdict(int)
        has_other = False
        for p, cnt in current_factors.items():
            decomposed = factorize(p + 1)
            for q, exp in decomposed:
                new_factors[q] += cnt * exp
                if q not in (2, 3):
                    has_other = True
        steps_done += 1
        current_factors = dict(new_factors)
        if steps_done >= K:
            break

    if steps_done == K:
        result = 1
        for p, e in current_factors.items():
            result = (result * pow(p, e, MOD)) % MOD
        print(result)
        return

    a2 = current_factors.get(2, 0)
    a3 = current_factors.get(3, 0)
    K_remaining = K - steps_done

    n_pairs = K_remaining // 2
    n_rem = K_remaining % 2
    phi = MOD - 1

    pow_2_pairs = pow(2, n_pairs, phi)
    if n_rem:
        e2 = (a3 * pow_2_pairs) % phi
        e2 = (e2 * 2) % phi
        e3 = (a2 * pow_2_pairs) % phi
    else:
        e2 = (a2 * pow_2_pairs) % phi
        e3 = (a3 * pow_2_pairs) % phi

    part2 = pow(2, e2, MOD)
    part3 = pow(3, e3, MOD)
    result = (part2 * part3) % MOD
    print(result)

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