結果

問題 No.2019 Digits Filling for All Substrings
ユーザー gew1fw
提出日時 2025-06-12 18:25:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,768 KB
実行使用メモリ 76,512 KB
最終ジャッジ日時 2025-06-12 18:26:30
合計ジャッジ時間 2,775 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

n = int(input())
s = input().strip()

# Compute sum_10k
current_sum = 0
sum_10k = 0
for c in s:
    if c == '?':
        current_sum = (current_sum * 10 + 10) % MOD
    else:
        current_sum = (current_sum + 1) % MOD
    sum_10k = (sum_10k + current_sum) % MOD

# Compute total_substrings
total_substrings = n * (n + 1) // 2 % MOD

# Compute total_part1
inv3 = pow(3, MOD-2, MOD)
total_part1 = (sum_10k - total_substrings) * inv3 % MOD

# Compute count_part2
current_sum = 0
freq = [0] * 3
freq[0] = 1
count_part2 = 0
for c in s:
    if c != '?':
        current_sum += int(c)
    current_sum_mod3 = current_sum % 3
    count_part2 = (count_part2 + freq[current_sum_mod3]) % MOD
    freq[current_sum_mod3] = (freq[current_sum_mod3] + 1) % MOD

# Final answer
answer = (total_part1 + count_part2) % MOD
print(answer)
0