結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー gew1fw
提出日時 2025-06-12 15:54:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,205 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 91,468 KB
最終ジャッジ日時 2025-06-12 15:54:41
合計ジャッジ時間 8,517 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
MOD = 998244353

def main():
    S = sys.stdin.readline().strip()
    from collections import Counter
    cnt = Counter(S)
    n = len(S)
    max_fact = n
    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

    polynomials = []
    for char, freq in cnt.items():
        p = [0] * (freq + 1)
        for c in range(freq + 1):
            p[c] = inv_fact[c]
        polynomials.append(p)
    
    current_poly = [1]
    for p in polynomials:
        new_poly = [0] * (len(current_poly) + len(p) - 1)
        for i in range(len(current_poly)):
            for j in range(len(p)):
                new_poly[i+j] = (new_poly[i+j] + current_poly[i] * p[j]) % MOD
        current_poly = new_poly[:n+1]
    
    result = 0
    for k in range(1, n+1):
        if k < len(current_poly):
            a_k = current_poly[k]
            term = a_k * fact[k] % MOD
            result = (result + term) % MOD
    print(result % MOD)

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