結果

問題 No.2058 Binary String
ユーザー lam6er
提出日時 2025-03-26 15:50:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 921 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,708 KB
実行使用メモリ 64,764 KB
最終ジャッジ日時 2025-03-26 15:51:57
合計ジャッジ時間 3,326 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    input = sys.stdin.read
    N, K = map(int, input().split())
    M = N - 1
    
    # Precompute factorial and inverse factorial
    max_fact = M
    fact = [1] * (max_fact + 1)
    for i in range(1, max_fact + 1):
        fact[i] = fact[i-1] * i % MOD
    
    inv_fact = [1] * (max_fact + 1)
    inv_fact[max_fact] = pow(fact[max_fact], MOD-2, MOD)
    for i in range(max_fact-1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
    
    ans = 0
    for m in range(0, M + 1):
        if m == 0:
            term = 0  # 0^K is 0 for K >= 1
        else:
            # Compute C(M, m)
            cm = fact[M] * inv_fact[m] % MOD
            cm = cm * inv_fact[M - m] % MOD
            # Compute m^K mod MOD
            mk = pow(m, K, MOD)
            term = cm * mk % MOD
        ans = (ans + term) % MOD
    
    print(ans)

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