[BOJ 7848] Random Gap

View as PDF

Submit solution

Points: 2
Time limit: 4.0s
Memory limit: 128M

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

The pseudo-random number genegators (or RNGs for short) are widely used in statistical calculations. One of the simplest and ubiquitious is the linear congruence RNG, that calculates the n-th random number Rn as Rn = (aRn - 1 + c) mod m, where a, c and m are constants. For example, the sequence for a = 15, c = 7, m = 100 and starting value R0 = 1 is 1, 22, 37, 62, 37, 62, ...</p>

Linear RNG is very fast, and can yield surprisinly good sequence of random numbers, given the right choice of constants. There are various measures of RNG quality, and your task is to calculate one of them, namely the longest gap between members of the sequence. More precisely, given the values of a, c, m, and R0, find such two elements Ri < Rj that:

  1. there are no other Rk Ri &le; Rk &le; Rj,
  2. the difference Rj &minus; Ri is maximal.
## 입력 형식

Input file contains integer numbers a c m R0.

출력 형식

Output file should contain the single number -- the maximal difference found.

예제 입력 1

15 7 100 1

예제 출력 1

25

예제 입력 2

2 0 127 5

예제 출력 2

26

Comments

There are no comments at the moment.