CF1097H.Mateusz and an Infinite Sequence

普及/提高-

通过率:0%

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, k1k \geq 1 , we define the sequence MkM_k to be the concatenation of the dd copies of Mk1M_{k-1} . However, each of them is altered slightly — in the ii -th of them ( 1id1 \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 MM_\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 MM_\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 MM_\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 BXB \geq X ) if and only if for all i{1,2,,n}i \in \{1, 2, \dots, n\} , we have BiXiB_i \geq X_i .

He now asks himself how many integers xx in the range [1,An+1][1, |A| - n + 1] there are such that B(Ax,Ax+1,Ax+2,,Ax+n1)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?

输入格式

The first line contains two integers dd and mm ( 2d202 \leq d \leq 20 , 2m602 \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 ( 0geni<m0 \leq \mathrm{gen}_i < 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 ( 1n300001 \leq n \leq 30000 ) — the length of the sequence BB . The fourth line contains nn integers BiB_i ( 0Bi<m0 \leq B_i < m ). The fifth line contains two integers ll and rr ( 1lr10181 \leq l \leq r \leq 10^{18} , rl+1nr-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 :

首页