Automata Embedding

시간 제한1초메모리 제한1024 MB

요약
길이 n인 문자열 가운데 KMP 실패 링크 오토마타를 평면에 교차 없이 그릴 수 있는 것의 개수를 C가지 문자로 세어 998244353으로 나눈 나머지를 구한다.
난이도

어려움10점 중 9점

유형
조합론, 동적 계획법, 문자열 매칭, 수학
정답자
아직 제출이 없습니다

문제

For a string SS of length nn, let S\[a..b]S\[a..b] denote the substring consisting of the characters from position aa to position bb (where 1≤a≤b≤n1\leq a\leq b\leq n). Also, the failure function f:\[0,n]→\[0,n−1]f:\[0,n]\rightarrow\[0 , n-1] of SS is defined as follows.

\[f(i) =\max(\{0\}\cup\{j\,\vert\, S[1..j] =S[i-j+1..i] ,\, 1\leq j<i\})\]

The KMP automaton made using the failure function of string SS denotes the following kind of automaton. The automaton has n+1n+1 states \[0..n]\[0..n], and for each state 0≤i≤n0\leq i\leq n, there exists exactly one transition from ii to f(i)f(i).

If a KMP automaton can be embedded on a plane, it means that if we map state ii to a point at (i,0)(i,0) on the plane, and draw all transitions i→f(i)i\rightarrow f(i) as arrows which do not cross the xx-axis on the plane, it is possible to draw all n+1n+1 arrows such that no arrows intersect except when they meet at endpoints.

Using an alphabet consisting of CC letters, find the number of strings of length nn whose KMP automaton can be embedded on a plane modulo 998,244,353998\\, 244\\, 353.

KMP automaton for the string S=‘abab‘S = `abab`

입력

The first line of input contains two space-separated integers nn and CC, denoting the length of the string and the number of letters in the alphabet respectively.

출력

The first line of output should contain the number of strings of length nn whose KMP automaton can be embedded on a plane modulo 998,244,353998\\, 244\\, 353.

제한

  • 1≤n≤10181\leq n\leq 10^{18}
  • 1≤C≤1091\leq C\leq 10^{9}

예제2

  1. 예제 1

    입력
    3 3
    
    예상 출력
    27
    
  2. 예제 2

    입력
    1000000000000000000 1000000000
    
    예상 출력
    609226805