random gap poj2381
Random Gap
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 3538 | Accepted: 871 |
Description
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, ...
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:
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:
- there are no other Rk that Ri ≤ Rk ≤ Rj,
- the difference Rj − Ri is maximal.
Input
Input contains integer numbers a c m R0.
0 ≤ a, c, R0 ≤ 107, 1 ≤ m ≤ 16000000, am + c < 232.
0 ≤ a, c, R0 ≤ 107, 1 ≤ m ≤ 16000000, am + c < 232.
Output
Output should contain the single number -- the maximal difference found.
Sample Input
15 7 100 1
Sample Output
25
#include<iostream> using namespace std; char lock[16000000]; int main() { __int64 i,a,c,m,r0,temp; while(scanf("%I64d%I64d%I64d%I64d",&a,&c,&m,&r0)!=EOF) { memset(lock,0,sizeof(lock)); temp=r0; while(!lock[temp]) { lock[temp]=1; temp=(a*temp+c)%m; } int flag=0,max=0,j; for(i=0;i<16000000;i++) { if(lock[i]&&flag) { if(j>max) max=j; j=1; } if(lock[i]&&flag==0) { flag=1; j=1; } if(lock[i]==0&&flag) j++; } cout<<max<<endl; } return 0; }

浙公网安备 33010602011771号