結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,992 KB
testcase_01 AC 146 ms
77,568 KB
testcase_02 AC 132 ms
76,636 KB
testcase_03 AC 117 ms
77,024 KB
testcase_04 AC 146 ms
90,368 KB
testcase_05 AC 139 ms
85,248 KB
testcase_06 AC 204 ms
77,644 KB
testcase_07 AC 220 ms
77,616 KB
testcase_08 AC 136 ms
105,472 KB
testcase_09 AC 100 ms
106,240 KB
testcase_10 AC 104 ms
106,624 KB
testcase_11 AC 111 ms
101,248 KB
testcase_12 AC 109 ms
100,480 KB
testcase_13 AC 184 ms
78,264 KB
testcase_14 AC 158 ms
78,080 KB
testcase_15 AC 154 ms
77,392 KB
testcase_16 AC 178 ms
77,460 KB
testcase_17 AC 177 ms
78,696 KB
testcase_18 AC 183 ms
78,348 KB
testcase_19 AC 170 ms
77,952 KB
testcase_20 AC 146 ms
77,852 KB
testcase_21 AC 155 ms
78,076 KB
testcase_22 AC 175 ms
78,028 KB
testcase_23 AC 157 ms
77,304 KB
testcase_24 AC 137 ms
77,464 KB
testcase_25 AC 169 ms
77,664 KB
testcase_26 AC 146 ms
77,912 KB
testcase_27 AC 122 ms
76,812 KB
testcase_28 AC 144 ms
77,772 KB
testcase_29 AC 130 ms
77,172 KB
testcase_30 AC 134 ms
77,704 KB
testcase_31 AC 131 ms
77,184 KB
testcase_32 AC 145 ms
78,392 KB
testcase_33 AC 147 ms
86,736 KB
testcase_34 AC 154 ms
92,176 KB
testcase_35 AC 151 ms
87,084 KB
testcase_36 AC 158 ms
88,448 KB
testcase_37 AC 151 ms
91,264 KB
testcase_38 AC 148 ms
87,988 KB
testcase_39 AC 148 ms
89,112 KB
testcase_40 AC 154 ms
89,472 KB
testcase_41 AC 162 ms
87,172 KB
testcase_42 AC 151 ms
90,368 KB
testcase_43 AC 257 ms
79,108 KB
testcase_44 AC 244 ms
78,312 KB
testcase_45 AC 252 ms
78,592 KB
testcase_46 AC 251 ms
78,840 KB
testcase_47 AC 256 ms
78,564 KB
testcase_48 AC 248 ms
79,080 KB
testcase_49 AC 258 ms
78,384 KB
testcase_50 AC 248 ms
78,852 KB
testcase_51 AC 250 ms
78,744 KB
testcase_52 AC 248 ms
78,592 KB
testcase_53 AC 110 ms
105,472 KB
testcase_54 AC 112 ms
105,344 KB
testcase_55 AC 110 ms
101,376 KB
testcase_56 AC 111 ms
105,088 KB
testcase_57 AC 118 ms
105,728 KB
testcase_58 AC 110 ms
100,864 KB
testcase_59 AC 117 ms
101,120 KB
testcase_60 AC 115 ms
105,728 KB
testcase_61 AC 116 ms
105,984 KB
testcase_62 AC 111 ms
106,624 KB
testcase_63 AC 118 ms
76,788 KB
testcase_64 AC 120 ms
76,440 KB
testcase_65 AC 126 ms
76,660 KB
testcase_66 AC 154 ms
77,112 KB
testcase_67 AC 139 ms
77,468 KB
testcase_68 AC 153 ms
77,020 KB
testcase_69 AC 141 ms
76,696 KB
testcase_70 AC 160 ms
77,676 KB
testcase_71 AC 155 ms
77,020 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