結果

問題 No.391 CODING WAR
ユーザー lam6er
提出日時 2025-03-20 21:11:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,021 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,504 KB
実行使用メモリ 63,256 KB
最終ジャッジ日時 2025-03-20 21:12:34
合計ジャッジ時間 2,717 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def main():
    import sys
    N, M = map(int, sys.stdin.readline().split())
    
    if M > N:
        print(0)
        return
    
    # Precompute factorial and inverse factorial modulo MOD
    fact = [1] * (M + 1)
    for i in range(1, M + 1):
        fact[i] = fact[i - 1] * i % MOD
    
    inv_fact = [1] * (M + 1)
    inv_fact[M] = pow(fact[M], MOD - 2, MOD)
    for i in range(M - 1, -1, -1):
        inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD
    
    result = 0
    for k in range(0, M + 1):
        # Compute sign (-1)^k mod MOD
        sign = pow(-1, k, MOD)
        # Compute combination C(M, k)
        comb = fact[M] * inv_fact[k] % MOD
        comb = comb * inv_fact[M - k] % MOD
        # Compute (M -k)^N mod MOD
        base = M - k
        power = pow(base, N, MOD)
        # Compute term
        term = sign * comb % MOD
        term = term * power % MOD
        # Add to result
        result = (result + term) % MOD
    
    print(result)

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