-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUVa00350_PseudoRandomNumbers.java
More file actions
48 lines (41 loc) · 1.16 KB
/
UVa00350_PseudoRandomNumbers.java
File metadata and controls
48 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 286 (350 - Pseudo-Random Numbers) */
/* SUBMISSION: 09086697 */
/* SUBMISSION TIME: 2011-07-27 22:39:29 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00350_PseudoRandomNumbers {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = 1;
while (true) {
String line = in.readLine();
StringTokenizer stk = new StringTokenizer(line);
int Z = Integer.parseInt(stk.nextToken());
int I = Integer.parseInt(stk.nextToken());
int M = Integer.parseInt(stk.nextToken());
int L = Integer.parseInt(stk.nextToken());
if (Z == 0 && I == 0 && M == 0 && L == 0)
break;
int len = 1;
List<Integer> sequence = new ArrayList<Integer>();
sequence.add(L);
while (true) {
L = (Z * L + I) % M;
int ind = Collections.binarySearch(sequence, L);
if (ind >= 0) {
len -= ind;
break;
}
sequence.add(L);
++len;
}
System.out.println("Case " + t + ": " + len);
++t;
}
in.close();
System.exit(0);
}
}