Increasing Speed Limits
Time limit5sMemory limit512 MB
Given a sequence generated by a small recurrence, count strictly increasing subsequences by position, modulo 1000000007.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Combinatorics, Prefix sum
- Solved
- No attempts yet
Problem
You were driving along a highway when the road police caught you for speeding. They had been following you for a while, and they were amazed that you kept accelerating the whole time without ever touching the brakes. Now you need an excuse that sounds plausible.
You decided to say "every speed limit sign I saw was in increasing order, so I kept accelerating". The officer laughs, tells you every sign placed along the stretch of highway you drove, in order, and says you could not have been lucky enough to see only the ones that happened to be in increasing order.
Estimate how much luck that would take. Count how many subsequences of the given sequence are strictly increasing. The empty subsequence does not count, since it would mean you looked at no sign at all.
Subsequences are distinguished by position, not by value. In the sequence , the strictly increasing subsequence with values is counted twice, because there are two ways to pick the .
Input
The first line contains the number of test cases . The test cases follow.
The first line of each test case contains , , , and , separated by single spaces. is the length of the speed limit sequence, and is the length of the generating array . The next lines contain the elements of , one integer per line, from to .
Using , , and , the following pseudocode prints the speed limit sequence in order. mod is the remainder operation.
for i = 0 to n-1
print A[i mod m]
A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z
The input is generated this way only to keep the input small. It has nothing to do with how the problem is solved.
Limits:
Output
For each test case, print one line in the form Case #T: S, where is the test case number and is the number of non-empty strictly increasing subsequences modulo .
Hint
With , , , , and , the pseudocode prints .