CF1097H.Mateusz and an Infinite Sequence

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A Thue-Morse-Radecki-Mateusz sequence (Thorse-Radewoosh sequence in short) is an infinite sequence constructed from a finite sequence gen\mathrm{gen} of length dd and an integer mm, obtained in the following sequence of steps:

  • In the beginning, we define the one-element sequence M0=(0)M_0=(0).
  • In the kk-th step, k≥1k \geq 1, we define the sequence MkM_k to be the concatenation of the dd copies of Mk−1M_{k-1}. However, each of them is altered slightly — in the ii-th of them (1≤i≤d1 \leq i \leq d), each element xx is changed to (x+geni)(modm)(x+\mathrm{gen}_i) \pmod{m}.

For instance, if we pick gen=(0,1,2)\mathrm{gen} = (0, \color{blue}{1}, \color{green}{2}) and m=4m = 4:

  • M0=(0)M_0 = (0),
  • M1=(0,1,2)M_1 = (0, \color{blue}{1}, \color{green}{2}),
  • M2=(0,1,2,1,2,3,2,3,0)M_2 = (0, 1, 2, \color{blue}{1, 2, 3}, \color{green}{2, 3, 0}),
  • M3=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,2,3,0,3,0,1,0,1,2)M_3 = (0, 1, 2, 1, 2, 3, 2, 3, 0, \color{blue}{1, 2, 3, 2, 3, 0, 3, 0, 1}, \color{green}{2, 3, 0, 3, 0, 1, 0, 1, 2}), and so on.

As you can see, as long as the first element of gen\mathrm{gen} is 00, each consecutive step produces a sequence whose prefix is the sequence generated in the previous step. Therefore, we can define the infinite Thorse-Radewoosh sequence M∞M_\infty as the sequence obtained by applying the step above indefinitely. For the parameters above, M∞=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,… )M_\infty = (0, 1, 2, 1, 2, 3, 2, 3, 0, 1, 2, 3, 2, 3, 0, 3, 0, 1, \dots).

Mateusz picked a sequence gen\mathrm{gen} and an integer mm, and used them to obtain a Thorse-Radewoosh sequence M∞M_\infty. He then picked two integers ll, rr, and wrote down a subsequence of this sequence A:=((M∞)l,(M∞)l+1,…,(M∞)r)A := ((M_\infty)_l, (M_\infty)_{l+1}, \dots, (M_\infty)_r).

Note that we use the 11-based indexing both for M∞M_\infty and AA.

Mateusz has his favorite sequence BB with length nn, and would like to see how large it is compared to AA. Let's say that BB majorizes sequence XX of length nn (let's denote it as B≥XB \geq X) if and only if for all i∈1,2,…,ni \in {1, 2, \dots, n}, we have Bi≥XiB_i \geq X_i.

He now asks himself how many integers xx in the range [1,∣A∣−n+1][1, |A| - n + 1] there are such that B≥(Ax,Ax+1,Ax+2,…,Ax+n−1)B \geq (A_x, A_{x+1}, A_{x+2}, \dots, A_{x+n-1}). As both sequences were huge, answering the question using only his pen and paper turned out to be too time-consuming. Can you help him automate his research?

托尔-摩尔斯-拉德茨基-马泰乌什序列(简称“托尔-拉德沃什序列”)是一种由长度为 dd 的有限序列 gen\mathrm{gen} 和整数 mm 构造出的无限序列,其构造步骤如下:

  • 初始时,定义单元素序列 M0=(0)M_0 = (0);
  • 在第 kk 步(k≥1k \geq 1),将序列 MkM_k 定义为 dd 个 Mk−1M_{k-1} 的拼接。但每个副本均作轻微修改:在第 ii 个副本中(1≤i≤d1 \leq i \leq d),其中每个元素 xx 均被替换为 (x+geni) mod m(x + \mathrm{gen}_i) \bmod m。

例如,若取 gen=(0,1,2)\mathrm{gen} = (0, \color{blue}{1}, \color{green}{2}) 且 m=4m = 4,则有:

  • M0=(0)M_0 = (0),
  • M1=(0,1,2)M_1 = (0, \color{blue}{1}, \color{green}{2}),
  • M2=(0,1,2,1,2,3,2,3,0)M_2 = (0, 1, 2, \color{blue}{1, 2, 3}, \color{green}{2, 3, 0}),
  • M3=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,2,3,0,3,0,1,0,1,2)M_3 = (0, 1, 2, 1, 2, 3, 2, 3, 0, \color{blue}{1, 2, 3, 2, 3, 0, 3, 0, 1}, \color{green}{2, 3, 0, 3, 0, 1, 0, 1, 2}),依此类推。

如你所见,只要 gen\mathrm{gen} 的首元素为 00,则每一步生成的新序列都以此前一步生成的序列为前缀。因此,我们可以将无限托尔-拉德沃什序列 M∞M_\infty 定义为上述过程无限进行后所得的序列。对上述参数,有
M∞=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,… )M_\infty = (0, 1, 2, 1, 2, 3, 2, 3, 0, 1, 2, 3, 2, 3, 0, 3, 0, 1, \dots)。

马泰乌什选取了一个序列 gen\mathrm{gen} 和一个整数 mm,并据此生成了托尔-拉德沃什序列 M∞M_\infty。随后他选取两个整数 ll、rr,并写下该序列的一个子序列
A:=((M∞)l, (M∞)l+1, …, (M∞)r)A := \big((M_\infty)_l,\ (M_\infty)_{l+1},\ \dots,\ (M_\infty)_r\big)。

注意:此处 M∞M_\infty 和 AA 均采用从 1 开始的下标。

马泰乌什有一个他钟爱的长度为 nn 的序列 BB,他希望比较 BB 与 AA 的大小关系。我们称:当且仅当对所有 i∈{1,2,…,n}i \in \{1, 2, \dots, n\} 均有 Bi≥XiB_i \geq X_i 时,序列 BB 控制(majorizes) 长度为 nn 的序列 XX(记作 B≥XB \geq X)。

现在他想知道自己有多少个整数 xx 满足 x∈[1, ∣A∣−n+1]x \in [1,\ |A| - n + 1],使得
B≥(Ax, Ax+1, Ax+2, …, Ax+n−1)B \geq (A_x,\ A_{x+1},\ A_{x+2},\ \dots,\ A_{x+n-1})。
由于两个序列规模极大,仅靠纸笔计算这一问题耗时过长。你能帮他将这项研究自动化吗?

输入格式

The first line contains two integers dd and mm (2≤d≤202 \leq d \leq 20, 2≤m≤602 \leq m \leq 60) — the length of the sequence gen\mathrm{gen} and an integer used to perform the modular operations. The second line contains dd integers geni\mathrm{gen}_i (0≤geni<m0 \leq \mathrm{gen}_i \lt m). It's guaranteed that the first element of the sequence gen\mathrm{gen} is equal to zero.

The third line contains one integer nn (1≤n≤300001 \leq n \leq 30000) — the length of the sequence BB. The fourth line contains nn integers BiB_i (0≤Bi<m0 \leq B_i \lt m). The fifth line contains two integers ll and rr (1≤l≤r≤10181 \leq l \leq r \leq 10^{18}, r−l+1≥nr-l+1 \geq n).

第一行包含两个整数 dd 和 mm(2≤d≤202 \leq d \leq 20,2≤m≤602 \leq m \leq 60)—— 分别表示序列 gen\mathrm{gen} 的长度以及用于执行模运算的整数。
第二行包含 dd 个整数 geni\mathrm{gen}_i(0≤geni<m0 \leq \mathrm{gen}_i \lt m)。保证序列 gen\mathrm{gen} 的第一个元素为零。

第三行包含一个整数 nn(1≤n≤300001 \leq n \leq 30000)—— 表示序列 BB 的长度。
第四行包含 nn 个整数 BiB_i(0≤Bi<m0 \leq B_i \lt m)。
第五行包含两个整数 ll 和 rr(1≤l≤r≤10181 \leq l \leq r \leq 10^{18},且 r−l+1≥nr-l+1 \geq n)。

输出格式

Print a single integer — the answer to the problem.

输出一个整数——该问题的答案。

输入输出样例

  • 输入#1

    2 2
    0 1
    4
    0 1 1 0
    2 21

    输出#1

    6
  • 输入#2

    3 4
    0 1 2
    2
    0 2
    6 11

    输出#2

    1

说明/提示

Thorse-Radewoosh sequence in the first example is the standard Thue-Morse sequence, so the sequence AA is as follows: 1101001100101101001011010011001011010010. Here are the places where the sequence BB majorizes AA:

第一个例子中的 Thorse-Radewoosh 序列即为标准的 Thue-Morse 序列,因此序列 AA 如下:1101001100101101001011010011001011010010。以下是序列 BB 主导(majorizes)序列 AA 的位置:

输入解题思路,AI测评打分。不知道怎么写?

首页