1.

/**
* Angewandte Mathematik, SS11
* Problem: 350 Pseudo-Random Numbers
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=286
*
* @author Markus Schöllner
* @author Andreas Maier
* @version 1.0, 10/04/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.192
*/

import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Z = input.nextInt();
int I = input.nextInt();
int M = input.nextInt();
int L = input.nextInt();
int startMod;
int mod;
int counterCase = 1;
int counterSequence;
boolean end;

while(Z != 0 && I != 0 && M != 0 && L !=0) {
end = false;
counterSequence = 1;
startMod = (Z*L+I)%M;
mod = startMod;

while(end == false) {
mod = (Z*mod+I)%M;
if(mod == startMod) {
System.out.println("Case "+counterCase+": "+counterSequence);
end = true;
}
counterSequence++;
}
counterCase++;
Z = input.nextInt();
I = input.nextInt();
M = input.nextInt();
L = input.nextInt();
}
}
}

---------------------------------------------
1.
/**
* FWP: Ausgewaehlte Probleme aus dem ACM (SS10)
*
* Method: Simulation
* Problem: 350 - Pseudo-RandomNumbers
* Accepted: 0.252
* @author Evgeni Pavlidis
*
*/
import java.io.*;
import java.util.*;

class Main {

private static Set<Integer> cycle = new HashSet<Integer>();

public static void main(String...args)
{
Scanner scanner = new Scanner(System.in);

int z,i,m,l, counter, caseNumber = 1;

while( (z = scanner.nextInt()) != 0)
{
i = scanner.nextInt();
m = scanner.nextInt();
l = scanner.nextInt();

cycle.clear();

counter = 0;
l = (z*l + i) % m;

do
{
cycle.add(l);
l = (z*l + i) % m;
counter++;

}while(!cycle.contains(l));

System.out.println("Case " + caseNumber++ + ": " + counter);
}
}
}