結果

問題 No.2019 Digits Filling for All Substrings
ユーザー asumo0729asumo0729
提出日時 2022-08-09 23:25:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 377 ms / 2,000 ms
コード長 1,132 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 81,672 KB
実行使用メモリ 96,904 KB
最終ジャッジ日時 2023-10-20 08:50:49
合計ジャッジ時間 6,900 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,400 KB
testcase_01 AC 40 ms
53,400 KB
testcase_02 AC 40 ms
53,400 KB
testcase_03 AC 39 ms
53,400 KB
testcase_04 AC 265 ms
92,636 KB
testcase_05 AC 197 ms
86,036 KB
testcase_06 AC 180 ms
84,452 KB
testcase_07 AC 145 ms
81,548 KB
testcase_08 AC 365 ms
90,444 KB
testcase_09 AC 370 ms
90,364 KB
testcase_10 AC 372 ms
90,356 KB
testcase_11 AC 377 ms
90,724 KB
testcase_12 AC 368 ms
90,340 KB
testcase_13 AC 368 ms
90,368 KB
testcase_14 AC 56 ms
64,240 KB
testcase_15 AC 56 ms
64,344 KB
testcase_16 AC 74 ms
72,572 KB
testcase_17 AC 59 ms
66,408 KB
testcase_18 AC 61 ms
66,428 KB
testcase_19 AC 40 ms
53,400 KB
testcase_20 AC 39 ms
53,400 KB
testcase_21 AC 39 ms
53,400 KB
testcase_22 AC 39 ms
53,400 KB
testcase_23 AC 40 ms
53,400 KB
testcase_24 AC 188 ms
85,772 KB
testcase_25 AC 248 ms
91,580 KB
testcase_26 AC 147 ms
82,076 KB
testcase_27 AC 67 ms
66,392 KB
testcase_28 AC 66 ms
66,392 KB
testcase_29 AC 240 ms
89,468 KB
testcase_30 AC 315 ms
96,904 KB
testcase_31 AC 307 ms
96,632 KB
testcase_32 AC 134 ms
80,536 KB
testcase_33 AC 245 ms
91,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

'''
ある区間を考える
その区間の?を除いた数の合計のmod3が
①0のとき
?の数=kとする
すると?の数の合計はk桁の数分とれる
(k=3なら0~999)
当然3の倍数のが1個多くとれるので
(10 ** k - 1) / 3 + 1
②1,2のとき
(10 ** k - 1) / 3

なので区間の中にある?の数分の10の累乗と10**-1の1の累乗がわかればいい
区間の中の?以外の数の和のmod3==0のときの区間の数を足せばいい(①の最後の+1の部分)
'''
mod = 998244353
N = int(input())
S = input() 
a = [0]
for s in S:
    if s == '?':
        a.append(a[-1] + 1)
    else:
        a.append(a[-1])
A = []
for i in range(N + 1):
    A.append(pow(10, a[i], mod))
acc = 0
ans = 0
for i in range(N + 1):
    ans += A[i] * acc
    acc += pow(A[i], mod - 2, mod)
    ans %= mod
ans -= (N + 1) * N // 2
ans %= mod
ans *= pow(3, mod - 2, mod)
ans %= mod

mod3 = [0 for _ in range(3)]
mod3[0] += 1
res = 0
for s in S:
    if s == '?':
        x = 0
    else:
        x = int(s)
    res += x
    mod3[res % 3] += 1
for x in mod3:
    ans += x * (x - 1) // 2
    ans %= mod
print(ans)
0