結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー tassei903tassei903
提出日時 2024-02-24 01:44:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 4,534 bytes
コンパイル時間 646 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 106,304 KB
最終ジャッジ日時 2024-02-24 01:44:27
合計ジャッジ時間 17,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,332 KB
testcase_01 AC 185 ms
76,852 KB
testcase_02 AC 150 ms
76,480 KB
testcase_03 AC 141 ms
76,172 KB
testcase_04 AC 166 ms
89,992 KB
testcase_05 AC 151 ms
84,404 KB
testcase_06 AC 223 ms
77,552 KB
testcase_07 AC 270 ms
77,396 KB
testcase_08 AC 125 ms
105,148 KB
testcase_09 AC 119 ms
106,304 KB
testcase_10 AC 123 ms
106,012 KB
testcase_11 AC 125 ms
101,248 KB
testcase_12 AC 124 ms
100,576 KB
testcase_13 AC 218 ms
78,324 KB
testcase_14 AC 209 ms
77,300 KB
testcase_15 AC 189 ms
77,292 KB
testcase_16 AC 210 ms
77,508 KB
testcase_17 AC 209 ms
77,804 KB
testcase_18 AC 215 ms
77,980 KB
testcase_19 AC 200 ms
77,456 KB
testcase_20 AC 183 ms
77,076 KB
testcase_21 AC 192 ms
77,428 KB
testcase_22 AC 212 ms
77,928 KB
testcase_23 AC 189 ms
76,960 KB
testcase_24 AC 169 ms
77,296 KB
testcase_25 AC 201 ms
77,308 KB
testcase_26 AC 171 ms
77,452 KB
testcase_27 AC 151 ms
76,104 KB
testcase_28 AC 186 ms
77,740 KB
testcase_29 AC 159 ms
76,584 KB
testcase_30 AC 164 ms
77,216 KB
testcase_31 AC 164 ms
76,692 KB
testcase_32 AC 179 ms
78,448 KB
testcase_33 AC 172 ms
86,072 KB
testcase_34 AC 182 ms
91,168 KB
testcase_35 AC 186 ms
86,456 KB
testcase_36 AC 186 ms
87,452 KB
testcase_37 AC 182 ms
90,756 KB
testcase_38 AC 181 ms
86,988 KB
testcase_39 AC 174 ms
88,572 KB
testcase_40 AC 180 ms
88,860 KB
testcase_41 AC 194 ms
86,724 KB
testcase_42 AC 183 ms
89,792 KB
testcase_43 AC 299 ms
79,340 KB
testcase_44 AC 298 ms
78,092 KB
testcase_45 AC 294 ms
78,660 KB
testcase_46 AC 294 ms
78,084 KB
testcase_47 AC 299 ms
78,736 KB
testcase_48 AC 293 ms
79,032 KB
testcase_49 AC 314 ms
78,656 KB
testcase_50 AC 296 ms
78,696 KB
testcase_51 AC 295 ms
78,652 KB
testcase_52 AC 300 ms
78,364 KB
testcase_53 AC 127 ms
104,920 KB
testcase_54 AC 131 ms
105,136 KB
testcase_55 AC 126 ms
101,080 KB
testcase_56 AC 130 ms
104,836 KB
testcase_57 AC 136 ms
105,328 KB
testcase_58 AC 126 ms
100,552 KB
testcase_59 AC 132 ms
100,804 KB
testcase_60 AC 129 ms
104,992 KB
testcase_61 AC 131 ms
105,180 KB
testcase_62 AC 132 ms
106,216 KB
testcase_63 AC 141 ms
76,312 KB
testcase_64 AC 139 ms
76,340 KB
testcase_65 AC 142 ms
76,340 KB
testcase_66 AC 173 ms
77,048 KB
testcase_67 AC 164 ms
76,716 KB
testcase_68 AC 168 ms
76,724 KB
testcase_69 AC 171 ms
76,724 KB
testcase_70 AC 182 ms
77,344 KB
testcase_71 AC 182 ms
76,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes")
no = lambda :print("no");No = lambda :print("No")
#######################################################################
mod = 998244353


for _ in range(ni()):
    n, m = na()
    x = na()
    m2 = pow(m-2, mod-2, mod)
    ss = 0
    for i in range(n):
        if x[i] != -1:
            ss -= x[i]
        ss %= m

    if ss != 0:
        dpc = [1, 0, 0]
        dps = [0, 0, 0]
        s = 0
        for i in range(n):
            ndps, ndpc = [0, 0, 0], [0, 0, 0]
            dps, ndps = ndps, dps
            dpc, ndpc = ndpc, dpc
            if x[i] != -1:
                if s == 0:
                    dps[0] += ndps[0]
                else:
                    dps[0] += ndpc[0] + ndps[0]
                if (s + ss) % m == 0:
                    dps[1] += ndps[1]
                else:
                    dps[1] += ndpc[1] + ndps[1]
                if s == 0 or (s + ss) % m == 0:
                    dps[2] += ndpc[2] + ndps[2]
                else:
                    dps[2] += ndpc[2] * (m-3) % mod * m2 + ndps[2]
                dps[0] %= mod
                dps[1] %= mod
                dps[2] %= mod
                
                dpc[0] = ndpc[0]
                dpc[1] = ndpc[1]
                dpc[2] = ndpc[2]
            else:
                if s == 0:
                    dps[0] += ndps[0]
                    dps[1] += ndps[0]
                    dps[2] += ndps[0]
                else:
                    dps[0] += ndpc[0] + ndps[0]
                    dps[1] += ndpc[0] + ndps[0]
                    dps[2] += ndpc[0] + ndps[0]
                if (s + ss) % m == 0:
                    dps[0] += ndps[1]
                    dps[1] += ndps[1]
                    dps[2] += ndps[1]
                else:
                    dps[0] += ndpc[1] + ndps[1]
                    dps[1] += ndpc[1] + ndps[1]
                    dps[2] += ndpc[1] + ndps[1]
                if s == 0 or (s + ss) % m == 0:
                    dps[0] += ndpc[2] * (m-2) + ndps[2] * (m-2)
                    dps[1] += ndps[2] * (m-2) + ndpc[2] * (m-2)
                    dps[2] += ndps[2] * (m-2) + ndpc[2] * (m-2)
                else:
                    dps[0] += ndpc[2] * (m-3) + ndps[2] * (m-2)
                    dps[1] += ndpc[2] * (m-3) + ndps[2] * (m-2)
                    dps[2] += ndpc[2] * (m-3) + ndps[2] * (m-2)
                dps[0] %= mod
                dps[1] %= mod
                dps[2] %= mod

                dpc[0] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2)
                dpc[1] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2)
                dpc[2] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2)
                dpc[0] %= mod
                dpc[1] %= mod
                dpc[2] %= mod
            if x[i] != -1:
                s += x[i]
            s %= m
        ans = dps[1]
    else:
        m1 = pow(m-1, mod-2, mod)
        dpc = [1, 0]
        dps = [0, 0]
        s = 0
        for i in range(n):
            ndps, ndpc = [0, 0], [0, 0]
            dps, ndps = ndps, dps
            dpc, ndpc = ndpc, dpc
            if x[i] != -1:
                if s == 0:
                    dps[0] += ndps[0]
                else:
                    dps[0] += ndpc[0] + ndps[0]
                if s == 0:
                    dps[1] += ndpc[1] + ndps[1]
                else:
                    dps[1] += ndpc[1] * (m-2) % mod * m1 + ndps[1]
                dps[0] %= mod
                dps[1] %= mod
                dpc[0] = ndpc[0]
                dpc[1] = ndpc[1]

            else:
                if s == 0:
                    dps[0] += ndps[0]
                    dps[1] += ndps[0]
                else:
                    dps[0] += ndpc[0] + ndps[0]
                    dps[1] += ndpc[0] + ndps[0]
                if s == 0:
                    dps[0] += ndpc[1] * (m-1) + ndps[1] * (m-1)
                    dps[1] += ndpc[1] * (m-1) + ndps[1] * (m-1)
                else:
                    dps[0] += ndpc[1] * (m-2) + ndps[1] * (m-1)
                    dps[1] += ndpc[1] * (m-2) + ndps[1] * (m-1)
                dps[0] %= mod
                dps[1] %= mod

                dpc[0] = ndpc[0] + ndpc[1] * (m-1)
                dpc[1] = ndpc[0] + ndpc[1] * (m-1)

                dpc[0] %= mod
                dpc[1] %= mod
            if x[i] != -1:
                s += x[i]
            s %= m
        ans = dps[0]
    print(ans%mod)
0