結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー tassei903
提出日時 2024-02-24 01:32:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,423 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 107,008 KB
最終ジャッジ日時 2024-09-29 09:43:54
合計ジャッジ時間 19,170 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 63
権限があれば一括ダウンロードができます

ソースコード

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]
                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
            #print(s, dps, dpc)
        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]
                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
            #print(dps, dpc)
        ans = dps[0]
    print(ans)
0