Linear Congruential Generator
Time limit2sMemory limit512 MB
Given a linear congruential generator and two index ranges, sum X_i mod (X_j+1) over all pairs i in the first range and j in the second.
- Level
Hard9 of 10
- Topics
- Math, Number theory, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
You are given a generator defined by the recurrence relation
where is the generated sequence of pseudorandom values, and , , , are integer constants which specify the generator.
Additionally, two integer intervals and are given. Please calculate
Input
The input contains several test cases. The first line contains an integer indicating the number of test cases. The following describes all test cases. For each test case:
The only line contains eight integers , , , , , , , .
Output
For each test case, output a line containing “Case #x: y” (without quotes), where x is the test case number starting from 1, and y is the answer to this test case.
Constraints
- The sum of in all test cases does not exceed .
Hint
In the first sample case, .
In the second sample case, .