結果
| 問題 |
No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 72 |
ソースコード
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)