結果

問題 No.1321 塗るめた
ユーザー lam6er
提出日時 2025-03-20 18:53:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,439 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 70,620 KB
最終ジャッジ日時 2025-03-20 18:54:56
合計ジャッジ時間 3,647 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    input = sys.stdin.read
    N, M, K = map(int, input().split())
    
    if K == 0:
        print(0)
        return
    
    max_fact = max(M, K)
    # Precompute factorial and inverse factorial modulo MOD
    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
    
    # Calculate C(M, K)
    if M < K:
        cmk = 0
    else:
        cmk = fact[M] * inv_fact[K] % MOD
        cmk = cmk * inv_fact[M - K] % MOD
    
    # Precompute M^N mod MOD
    pow_m = pow(M, N, MOD)
    
    sum_terms = 0
    for i in range(0, K+1):
        # Compute C(K, i)
        comb_ki = fact[K] * inv_fact[i] % MOD
        comb_ki = comb_ki * inv_fact[K - i] % MOD
        
        # (-1)^i mod MOD
        term_sign = 1 if i % 2 == 0 else MOD -1
        
        base = (M + K - i) % MOD
        a = pow(base, N, MOD)
        b = pow_m
        
        current = (a - b) % MOD
        current = (current + MOD) % MOD  # Ensure non-negative
        
        term = (term_sign * comb_ki) % MOD
        term = (term * current) % MOD
        
        sum_terms = (sum_terms + term) % MOD
    
    ans = (cmk * sum_terms) % MOD
    print(ans)

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