luogu#P9129. [USACO23FEB] Piling Papers G

[USACO23FEB] Piling Papers G

题目描述

Farmer John wrote down N(1N300)N (1 \le N \le 300) digits on pieces of paper. For each i[1,N]i \in [1,N], the ith piece of paper contains digit ai(1ai9)a_i (1 \le a_i \le 9).

The cows have two favorite integers AA and B(1AB<1018)B(1 \le A \le B<10^{18}), and would like you to answer Q(1Q5104)Q (1 \le Q \le 5⋅10^4) queries. For the ii-th query, the cows will move left to right across papers liri(1liriN)l_i \cdots r_i (1 \le l_i \le r_i \le N), maintaining an initially empty pile of papers. For each paper, they will either add it to the top of the pile, to the bottom of the pile, or neither. In the end, they will read the papers in the pile from top to bottom, forming an integer. Over all 3rili+13 ^ {r_i−l_i+1} ways for the cows to make choices during this process, count the number of ways that result in the cows reading an integer in [A,B][A,B] inclusive, and output this number modulo 109+710^9+7.

输入格式

The first line contains three space-separated integers N,AN, A, and BB.

The second line contains NN space-separated digits a1,a2,,aNa_1,a_2, \cdots ,a_N.

The third line contains an integer QQ, the number of queries.

The next QQ lines each contain two space-separated integers lil_i and rir_i.

输出格式

For each query, a single line containing the answer.

题目大意

给定长度为 n(1n300)n(1\leq n\leq 300) 的整数序列 a(1ai9)a(1\leq a_i\leq 9),和整数区间 [A,B](1AB1018)[A,B](1\leq A\leq B\leq 10^{18}),有 qq 次询问,每次询问给出 l,rl,r。每次询问开始,你有一个空串 xx,你可以正序对 alra_{l\sim r} 进行操作,操作有三种:xx+aix\rightarrow\overline{x+a_i}xai+xx\rightarrow\overline{a_i+x},或者什么也不做,求 x\overline{x} 的取值在 [A,B][A,B] 的不同的操作方案数,对 109+710^9+7 取模。

5 13 327
1 2 3 4 5
3
1 2
1 3
2 5
2
18
34

提示

Explanation for Sample 1

For the first query, there are nine ways Bessie can stack papers when reading the interval [1,2][1,2]:

  • Bessie can ignore 11 then ignore 22, getting 00.
  • Bessie can ignore 11 then add 22 to the top of the stack, getting 22.
  • Bessie can ignore 11 then add 22 to the bottom of the stack, getting 22.
  • Bessie can add 11 to the top of the stack then ignore 22, getting 11.
  • Bessie can add 11 to the top of the stack then add 22 to the top of the stack, getting 2121.
  • Bessie can add 11 to the top of the stack then add 22 to the bottom of the stack, getting 1212.
  • Bessie can add 11 to the bottom of the stack then ignore 22, getting 11.
  • Bessie can add 11 to the bottom of the stack then add 22 to the top of the stack, getting 2121.
  • Bessie can add 11 to the bottom of the stack then add 22 to the bottom of the stack, getting 1212.

Only the 22 ways that give 2121 yield a number between 1313 and 327327, so the answer is 22.

SCORING

  • Inputs 232-3: B<100B<100
  • Inputs 454-5: A=BA=B
  • Inputs 6136-13: No additional constraints.