#P5330. rolling

rolling

题目描述

给定整数 P,BP,B 满足 PP 是质数,1≤B≤P−11\le B\le P-1。

对于序列 X=(x1,x2,⋯ ,xn)X=(x_1,x_2,\cdots,x_n),定义 hash⁡(X)\operatorname{hash}(X) 的值为

$$\operatorname{hash}(X)=\left(\sum_{i=1}^nx_iB^{n-i}\right)\bmod P $$

给定 MM 对整数 (Li,Ri)(1≤i≤M)(L_i,R_i)(1\le i\le M),请问是否存在长度为 NN 的序列 A=(A1,A2,⋯ ,AN)A=(A_1,A_2,\cdots,A_N) 满足:

  • 对每一个 i(1≤i≤M)i(1\le i\le M),都有$$\operatorname{hash}((A_{L_i},A_{L_i+1},\cdots,A_{R_i}))\not=0 $$

输入格式

第一行四个整数 P,B,N,MP,B,N,M,接下来 MM 行每行两个整数 Li,RiL_i,R_i。

输出格式

若存在 AA 满足要求输出 Yes,否则输出 No。

样例 1 解释

序列 A=(2,0,4)A=(2,0,4) 满足要求。其中

  • hash⁡((A1))=2\operatorname{hash}((A_1))=2
  • hash⁡((A1,A2))=1\operatorname{hash}((A_1,A_2))=1
  • hash⁡((A2,A3))=1\operatorname{hash}((A_2,A_3))=1

输入输出样例 #1

输入 #1

3 2 3 3
1 1
1 2
2 3

输出 #1

Yes

输入输出样例 #2

输入 #2

2 1 3 3
1 1
2 3
1 3

输出 #2

No

输入输出样例 #3

输入 #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

输出 #3

Yes

说明/提示