Universal Cup Judging System

Universal Cup

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓
Statistics

在 KMP 演算法中,next 陣列(也稱為 LPS 陣列,即最長共同前後綴陣列,或失配函數)是一個核心概念。給定一個長度為 $n$ 的字串 $S = s_1s_2\dots s_n$,其 next 陣列 $next[1..n]$ 的定義為:$next[i]$ 表示子字串 $S[1..i]$ 的最長相同真前綴與真後綴的長度,特別地,$next[1] = 0$。形式化地,其定義為:

$$next[i] = \max_{k=0,\dots,i-1} \{k : S[1\dots k] = S[i-k+1\dots i]\}$$

這裡,$S[l..r]$ 定義為字串 $S$ 中從第 $l$ 個字元到第 $r$ 個字元構成的子字串。我們認為對於 $\forall 1 \le i \le n$,$S[1\dots 0] = S[i+1\dots i]$,它們均為空字串。

給定一個長度為 $n$ 的陣列 $A[1..n]$。你需要計算有多少個定義在字元集 $\Sigma$ 上、長度為 $n$ 的不同字串 $S$,其 next 陣列恰好等於給定的陣列 $A$。字元集的大小為 $m$。請輸出滿足條件的字串數量模 $998\,244\,353$ 的值。

輸入格式

單個輸入檔案中包含多組測試資料。輸入的第一行包含一個整數 $T$($1 \le T \le 10^5$),表示測試資料的組數。

對於每組測試資料:

  • 第一行包含兩個整數 $n$($2 \le n \le 10^5$)和 $m$($1 \le m \le 10^6$),分別表示給定陣列的長度和字元集的大小。
  • 第二行包含 $n$ 個以空格分隔的整數,表示給定的陣列 $A$。保證對於 $\forall 1 \le i \le n$,有 $0 \le A_i \le i - 1$。

保證所有測試資料的 $n$ 總和設不超過 $5 \times 10^5$,即 $\sum n \le 5 \times 10^5$。

輸出格式

對於每組測試資料,輸出一行一個整數,表示滿足條件的字串數量模 $998\,244\,353$ 的值。

範例

輸入

3
3 3
0 1 2
7 3
0 0 0 1 2 3 0
7 3
0 0 0 1 2 3 1

輸出

3
24
0

說明

對於範例中的第二組資料,假設字元集 $\Sigma = \{'a', 'b', 'c'\}$。滿足要求的 $24$ 個字串為:

abbabbb abbabbc abcabcb abcabcc acbacbb acbacbc
accaccb accaccc baabaaa baabaac bacbaca bacbacc
bcabcaa bcabcac bccbcca bccbccc caacaaa caacaab
cabcaba cabcabb cbacbaa cbacbab cbbcbba cbbcbbb

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.