#P6009. [USACO20JAN] Non-Decreasing Subsequences P
[USACO20JAN] Non-Decreasing Subsequences P
Problem Description
Bessie recently took part in a USACO contest and encountered the following problem. Of course, Bessie knows how to solve it. Do you?
Consider a sequence of length , , consisting only of integers in the range () (). You are given () queries of the form (). For each query, compute the number of non-decreasing subsequences in modulo .
A non-decreasing subsequence of is a sequence of indices such that and . Make sure you also count the empty subsequence.
Input Format
The first line contains two space-separated integers and .
The second line contains space-separated integers .
The third line contains an integer .
The next lines each contain two space-separated integers and .
Output Format
For each query , output on a new line the number of non-decreasing subsequences in modulo .
5 2
1 2 1 1 2
3
2 3
4 5
1 5
3
4
20
Hint
Sample Explanation
For the first query, the non-decreasing subsequences are , , and . is not a non-decreasing subsequence because .
For the second query, the non-decreasing subsequences are , , , and .
Subtasks
- Test points satisfy .
- Test points satisfy .
- Test points satisfy .
- Test points have no additional constraints.
Translated by ChatGPT 5