Ultraviolet A 350-pseudo-random numbers

Source: Internet
Author: User
Tags modulus

Pseudo-Random Numbers


Computers normally cannot generate really random numbers, but frequently are used to generate sequences of pseudo-random numbers. these are generated by some algorithm, but appear for all practical purposes to be really random. random numbers are used in
Applications, including simulation.

A common pseudo-random number generation technique is called the linear congruential method. If the last pseudo-random number generated was
L, Then the next number is generated by evaluating (, where
ZIs a constant multiplier,IIs a constant increment, and
M
Is a constant modulus. For example, supposeZIs 7,IIs 5, and
MIs 12. If the first random number (usually calledSeed) Is 4, then we can determine the next few pseudo do-random numbers are follows:

As you can see, the sequence of pseudo-random numbers generated by this technique repeats after six numbers. it shoshould be clear that the longest sequence that can be generated using this technique is limited by the modulus,
M.

In this problem you will be given sets of valuesZ,I,
M
, And the seed,L. Each of these will have no more than four digits. for each such set of values you are to determine the length of the cycle of pseudo-random numbers that will be generated. but be careful: the cycle might not begin with
Seed!

Input

Each input line will contain four integer values, in order,Z,
I
,M, AndL. The last line will contain in four zeroes, and marks the end of the input data.
LWill be lessM.

Output

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

Sample Input

7 5 12 45173 3849 3279 15119111 5309 6000 12341079 2136 9999 12370 0 0 0

Sample output

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

When the third group outputs 501, it is too careless to submit the statement without seeing it... the question also implies that the loop does not necessarily start from the first one--I am still too disconnected.

# Include <stdio. h>

Void main ()
{Int Z, I, m, l, I, a [10000], cycle [10000], K, sum, num = 0;
While (scanf ("% d", & Z, & I, & M, & L ))
{If (Z + I + M + L = 0) break;
Sum = 0; k = 0; ++ num;
For (I = 0; I <m; I ++) A [I] = 0;
While (A [l] = 0)
{A [l] = 1; ++ sum;
Cycle [Sum] = L;
L = (z * L + I) % m;
}
If (L! = Cycle [1]) {While (cycle [k + 1]! = L) ++ K ;}
Printf ("case % d: % d \ n", num, Sum-k );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.