Logo
(追記) (追記ここまで)

31349번 - Inverse KMP 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB66282142.857%

문제

bobo has just learnt Knuth-Morris-Pratt (KMP) algorithm.

For string $S = s_{1} s_{2} \dots s_{n},ドル $\mathrm{KMP}(S) = (f_2, f_3, \dots, f_n)$ where $f_i$ is the maximum $j < i$ where $s_{1} s_{2} \dots s_{j} = s_{i - j + 1} s_{i - j + 2} \dots s_{i}$.

Given $f_2, f_3, \dots, f_n$ and the size of alphabet, find out the number of strings $S$ where $\mathrm{KMP}(S) = (f_2, f_3, \dots, f_n)$ modulo $(10^9 + 7)$.

입력

The first line contains 2ドル$ integers $n$ and $c,ドル which denotes the length of the string and the size of alphabet, respectively (2ドル \leq n \leq 2 \cdot 10^5, 1 \leq c \leq 10^9$).

The second line contains $(n - 1)$ integers $f_2, f_3, \dots, f_n$ (0ドル \leq f_i < i$).

It is guaranteed that there exists at least one solution.

출력

A single integer denotes the number of strings.

제한

예제 입력 1

3 3
0 0

예제 출력 1

12

예제 입력 2

5 1000000000
1 2 3 4

예제 출력 2

1000000000

힌트

출처

Contest > Open Cup > 2014/2015 Season > Stage 10: Grand Prix of China D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /