結果

問題 No.1659 Product of Divisors
ユーザー lam6er
提出日時 2025-03-20 20:22:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 82,716 KB
実行使用メモリ 59,176 KB
最終ジャッジ日時 2025-03-20 20:24:28
合計ジャッジ時間 1,890 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def factor(n):
    factors = {}
    while n % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        n = n // 2
    i = 3
    while i * i <= n:
        while n % i == 0:
            factors[i] = factors.get(i, 0) + 1
            n = n // i
        i += 2
    if n > 1:
        factors[n] = 1
    return factors

def main():
    N, K = map(int, input().split())
    if N == 1:
        print(1)
        return
    
    factors = factor(N)
    if not factors:
        print(1)
        return
    
    max_a = max(factors.values())
    # Precompute factorial and inverse factorial up to max_a
    fact = [1] * (max_a + 1)
    for i in range(1, max_a + 1):
        fact[i] = fact[i-1] * i % MOD
    inv_fact = [1] * (max_a + 1)
    inv_fact[max_a] = pow(fact[max_a], MOD-2, MOD)
    for i in range(max_a -1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
    
    ans = 1
    for p, a in factors.items():
        m = K + a
        r = m % MOD
        if r < a:
            ans = 0
            break
        # Compute product (r)*(r-1)* ... * (r -a +1)
        product = 1
        for i in range(a):
            product = product * (r - i) % MOD
        contribution = product * inv_fact[a] % MOD
        ans = ans * contribution % MOD
    print(ans)

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