Random Gap
Time limit4sMemory limit128 MB
Given a linear congruential generator, find the largest gap between neighboring distinct values the sequence produces.
- Level
Medium5 of 10
- Topics
- Simulation, Hash map, Array, Sorting
- Solved
- No attempts yet
Problem
Pseudo-random number generators (RNGs) are widely used in statistical computing. One of the simplest and most common is the linear congruential generator, which produces the -th number from the previous one:
where , , and are fixed constants and is the starting seed. For example, with , , , and the sequence is
Such generators are fast and, with well-chosen constants, produce good random sequences. One measure of quality is the longest gap between the values the sequence produces. Consider the set of distinct values that appear in the sequence, and find two of them such that:
- no produced value satisfies (that is, and are neighbours among the distinct values), and
- the difference is as large as possible.
Output that maximal difference. If the sequence produces only one distinct value, output .
Input
The input contains four integers , , , and .
Output
Output a single integer: the maximal difference described above.