結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー lam6er
提出日時 2025-03-20 21:04:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 602 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 59,944 KB
最終ジャッジ日時 2025-03-20 21:04:42
合計ジャッジ時間 1,712 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

N, P = map(int, input().split())

# Compute e: exponent of P in N! using Legendre's formula
sum_e = 0
current_p = P
while current_p <= N:
    sum_e += N // current_p
    current_p *= P

if sum_e == 0:
    print(0)
else:
    # Compute a = N! mod MOD
    a = 1
    for i in range(2, N + 1):
        a = (a * i) % MOD
    
    # Compute b = N! mod (MOD-1)
    mod_m1 = MOD - 1
    b = 1
    for i in range(2, N + 1):
        b = (b * i) % mod_m1
    
    # Compute a^b mod MOD
    power = pow(a, b, MOD)
    
    # Calculate the final answer
    ans = (sum_e * power) % MOD
    print(ans)
0