1995: [USACO 2023 February Contest Gold] Problem 3. Piling Papers
题目描述
Farmer John wrote down () digits on pieces of paper. For each , the th 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 .
输入
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 .
输出
样例输入
5 13 327
1 2 3 4 5
3
1 2
1 3
2 5
样例输出
2
18
34
提示
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 .
SCORING:
- Inputs 2-3:
- Inputs 4-5:
- Inputs 6-13: No additional constraints.