350-pseudo-random Numbers

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_ problem&problem=286

Computers normally cannot generate really random numbers, but frequently are used to generate sequences of pseudo-random Umbers. These are generated by some algorithm, but appear the for all practical purposes to be really. Random numbers are used in many applications, including simulation.

A common pseudo-random number generation technique is called the linear. If the last pseudo-random number generated be L, then the next number is generated by evaluating ( , where Z is A constant multiplier, I am a constant increment, and M is a constant modulus. For example, suppose Z are 7, I is 5, and M are 12. If the random number (usually called the Seed) is 4, then we can determine the next few pseudo-random numbers are fo Llows:

As you can, the sequence of pseudo-random numbers generated to technique repeats after six. It should be clear to the longest sequence that can generated using this technique was limited by the modulus, M.

In this problem you are given sets of values for Z, I, M, and the seed, L. Each of these would have no more than four digits. For each such set of values you are to determine the length of the cycle of pseudo-random, that'll be numbers. But be careful:the cycle might not begin with the seed!

Input

Each input line would contain four integer values, in order, for Z, I, M, and L. The last line would contain four zeroes, and marks the end of the input data. L'll be less than M.

Output

For each input line, display the "Case number" (they are sequentially numbered, starting with 1) and the length of the Seque nCE of pseudo-random numbers before the sequence is repeated.

Sample Input

7 5 4
5173 3849 3279 1511 9111 5309 6000 1234 1079 2136 9999 1237
0 0 0 0

Sample Output

Case 1:6 case
2:546 case
3:500 case
4:220

Note: Loops do not necessarily return to the beginning, such as in Sample 3 (3111*1234+5309)%6000= (3111*5234+5309)%6000, which loops back to the second number.

Complete code:

/*0.015s*/
#include <cstdio>
#include <cstring>
int father[10000];
BOOL vis[10000];
int main ()
{
int cas = 0, K, B, M, x, temp, count;
while (scanf ("%d%d%d%d", &k, &b, &m, &x), k | | | b | | m | | x)
{
memset (father, 0, sizeof (father)); C11/>memset (Vis, 0, sizeof (VIS));
Vis[x] = true;
while (true)
{
temp = x;
x = (k * x + b)% M;
FATHER[X] = temp;
if (vis[x]) break;
Vis[x] = true;
}
temp = x;
Count = 0;
Do
{
x = father[x];
++count;
}
while (x!= temp);
printf ("Case%d:%d\n", ++cas, Count);
}
return 0;
}

But the truth is that loops either go back to the starting point or back to the second number:

/*0.012s*/
#include <cstdio>
int main ()
{
int cas = 0, K, B, M, X, Beg, BEG2, Count;
while (scanf ("%d%d%d%d", &k, &b, &m, &x), k | | | b | | m | | x)
{
k%= m, b%= m;
Beg = x;
BEG2 = x = (k * x + b)% M;
Count = 1;
while (x!= Beg)
{
x = (k * x + b)% M;
if (x = = BEG2) break;
++count;
}
printf ("Case%d:%d\n", ++cas, Count);
}
return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/