题目收集
题目列表题单
返回列表

Shortest Unique Prefix

CodeforcesP0025· 3000ms· 1024 MB· ZJU Summer 2026 Contest 2原题链接
字符串计数

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

Let S=S1S2⋯SnS = S_1 S_2 \cdots S_nS=S1​S2​⋯Sn​ be a string of length nnn. For indices 1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤n, let S[l..r]S[l..r]S[l..r] denote the substring SlSl+1⋯SrS_lS_{l+1}\cdots S_rSl​Sl+1​⋯Sr​.

For 1≤l≤n1 \le l \le n1≤l≤n, the prefix S[1..l]S[1..l]S[1..l] is said to be unique if it occurs in SSS only as a prefix; that is, there is no index ppp with 2≤p≤n−l+12 \le p \le n-l+12≤p≤n−l+1 satisfying S[p..p+l−1]=S[1..l]S[p..p+l-1] = S[1..l]S[p..p+l−1]=S[1..l].

The prefix S[1..n]=SS[1..n] = SS[1..n]=S is always unique, so every string has at least one unique prefix. Define the uniqueness u(S)u(S)u(S) of SSS as the length of its shortest unique prefix,

u(S)=min⁡{ l:1≤l≤n, S[1..l] is unique },u(S) = \min\{\, l : 1 \le l \le n,\ S[1..l]\ \text{is unique} \,\},u(S)=min{l:1≤l≤n, S[1..l] is unique},

so that 1≤u(S)≤n1 \le u(S) \le n1≤u(S)≤n for every SSS.

Given three integers nnn, kkk, and ppp, consider the knk^{n}kn strings of length nnn over an alphabet of kkk distinct symbols. Compute

(∑Su(S)) mod p,\left( \sum_{S} u(S) \right) \bmod p,(S∑​u(S))modp,

where the sum is taken over all knk^{n}kn such strings.

输入格式

A single line containing three integers nnn, kkk, and ppp.

1≤n≤801 \le n \le 801≤n≤80, 1≤k≤1001 \le k \le 1001≤k≤100, 108≤p≤10910^{8} \le p \le 10^{9}108≤p≤109. It is not guaranteed that ppp is prime.

输出格式

A single integer: the value of (∑Su(S)) mod p\bigl(\sum_{S} u(S)\bigr) \bmod p(∑S​u(S))modp.

样例输入 #1

2 3 998244353

样例输出 #1

12

样例输入 #2

3 1 998244353

样例输出 #2

3

样例输入 #3

20 5 998244353

样例输出 #3

311627017