#P9129. [USACO23FEB] Piling Papers G
[USACO23FEB] Piling Papers G
Description
Farmer John wrote down digits on pieces of paper. For each , the ith piece of paper contains digit .
The cows have two favorite integers and , and would like you to answer queries. For the -th query, the cows will move left to right across papers , 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 ways for the cows to make choices during this process, count the number of ways that result in the cows reading an integer in inclusive, and output this number modulo .
Input Format
The first line contains three space-separated integers , and .
The second line contains space-separated digits .
The third line contains an integer , the number of queries.
The next lines each contain two space-separated integers and .
Output Format
For each query, a single line containing the answer.
5 13 327
1 2 3 4 5
3
1 2
1 3
2 5
2
18
34
Hint
Explanation for Sample 1
For the first query, there are nine ways Bessie can stack papers when reading the interval :
- Bessie can ignore then ignore , getting .
- Bessie can ignore then add to the top of the stack, getting .
- Bessie can ignore then add to the bottom of the stack, getting .
- Bessie can add to the top of the stack then ignore , getting .
- Bessie can add to the top of the stack then add to the top of the stack, getting .
- Bessie can add to the top of the stack then add to the bottom of the stack, getting .
- Bessie can add to the bottom of the stack then ignore , getting .
- Bessie can add to the bottom of the stack then add to the top of the stack, getting .
- Bessie can add to the bottom of the stack then add to the bottom of the stack, getting .
Only the ways that give yield a number between and , so the answer is .
SCORING
- Inputs :
- Inputs :
- Inputs : No additional constraints.
京公网安备 11011102002149号