#arc171d. D - Rolling Hash

D - Rolling Hash

Score: 600600 points

问题陈述

给定非负整数 PPBB。这里,PP 是素数,1BP11 \leq B \leq P-1。 对于非负整数序列 X=(x1,x2,,xn)X=(x_1,x_2,\dots,x_n),哈希值 hash(X)\mathrm{hash}(X) 定义如下。

$\displaystyle \mathrm{hash}(X) = \left(\sum_{i=1}^n x_i B^{n-i}\right) \bmod P$

给定 MM 对整数 (L1,R1),(L2,R2),,(LM,RM)(L_1, R_1), (L_2, R_2), \dots, (L_M, R_M)。 是否存在长度为 NN 的非负整数序列 A=(A1,A2,,AN)A=(A_1, A_2, \dots, A_N),满足以下条件?

  • 对于所有 ii (1iM)(1 \leq i \leq M),以下条件成立:
    • ss 是通过取 AA 的第 LiL_i 个到第 RiR_i 个元素得到的序列 (ALi,ALi+1,,ARi)(A_{L_i}, A_{L_i + 1}, \dots, A_{R_i})。那么,hash(s)0\mathrm{hash}(s) \neq 0

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given non-negative integers PP and BB. Here, PP is prime, and 1BP11 \leq B \leq P-1.
For a sequence of non-negative integers X=(x1,x2,,xn)X=(x_1,x_2,\dots,x_n), the hash value hash(X)\mathrm{hash}(X) is defined as follows.

$\displaystyle \mathrm{hash}(X) = \left(\sum_{i=1}^n x_i B^{n-i}\right) \bmod P$

You are given MM pairs of integers (L1,R1),(L2,R2),,(LM,RM)(L_1, R_1), (L_2, R_2), \dots, (L_M, R_M).
Is there a sequence of non-negative integers A=(A1,A2,,AN)A=(A_1, A_2, \dots, A_N) of length NN that satisfies the condition below?

  • For all ii (1iM)(1 \leq i \leq M), the following condition holds:
    • Let ss be the sequence (ALi,ALi+1,,ARi)(A_{L_i}, A_{L_i + 1}, \dots, A_{R_i}) obtained by taking the LiL_i-th to the RiR_i-th elements of AA. Then, hash(s)0\mathrm{hash}(s) \neq 0.

Constraints

  • 2P1092 \leq P \leq 10^9
  • PP is prime.
  • 1BP11 \leq B \leq P - 1
  • 1N161 \leq N \leq 16
  • 1MN(N+1)21 \leq M \leq \frac{N(N+1)}{2}
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • (Li,Ri)(Lj,Rj)(L_i, R_i) \neq (L_j, R_j) if iji \neq j.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

PP BB NN MM

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LML_M RMR_M

Output

If there is a sequence that satisfies the condition in the problem statement, print Yes; otherwise, print No.

Sample Input 1

3 2 3 3
1 1
1 2
2 3

Sample Output 1

Yes

The sequence A=(2,0,4)A = (2, 0, 4) satisfies the condition because $\mathrm{hash}((A_1)) = 2, \mathrm{hash}((A_1, A_2)) = 1, \mathrm{hash}((A_2, A_3)) = 1$.

Sample Input 2

2 1 3 3
1 1
2 3
1 3

Sample Output 2

No

No sequence satisfies the condition.

Sample Input 3

998244353 986061415 6 11
1 5
2 2
2 5
2 6
3 4
3 5
3 6
4 4
4 5
4 6
5 6

Sample Output 3

Yes