結果
| 問題 | 
                            No.1964 sum = length
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-03-31 17:22:44 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 75 ms / 2,000 ms | 
| コード長 | 1,436 bytes | 
| コンパイル時間 | 230 ms | 
| コンパイル使用メモリ | 82,900 KB | 
| 実行使用メモリ | 63,892 KB | 
| 最終ジャッジ日時 | 2025-03-31 17:23:35 | 
| 合計ジャッジ時間 | 3,372 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 40 | 
ソースコード
MOD = 998244353
n = int(input())
max_s = n - 1
# Precompute g(s) for each s from 0 to max_s
g = [0] * (max_s + 1)
for s in range(max_s + 1):
    count = 0
    d = 1
    while True:
        lower = 10 ** (d - 1) - d
        if lower > s:
            break
        upper = (10 ** d) - d - 1
        if s <= upper:
            count += 1
        d += 1
    g[s] = count
# Initialize DP table
dp = [[0] * (n) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    for current_sum in range(n):
        current = dp[i][current_sum]
        if current == 0:
            continue
        
        if i == n - 1:
            # Last element, s must be exactly (n-1 - current_sum)
            s = (n - 1) - current_sum
            if s < 0 or s > max_s:
                continue
            ways = g[s]
            if ways == 0:
                continue
            new_sum = current_sum + s
            if new_sum < n:
                dp[i+1][new_sum] = (dp[i+1][new_sum] + current * ways) % MOD
        else:
            max_s_possible = (n - 1) - current_sum
            for s in range(0, max_s_possible + 1):
                if s > max_s:
                    continue
                ways = g[s]
                if ways == 0:
                    continue
                new_sum = current_sum + s
                if new_sum < n:
                    dp[i+1][new_sum] = (dp[i+1][new_sum] + current * ways) % MOD
print(dp[n][n-1] % MOD)
            
            
            
        
            
lam6er