Pseudo-Random Numbers

   Pseudo-Random Numbers

    Time Limit:1000MS Memory Limit:30000KB

Description 
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 many 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 ( Z x L + I ) mod M, where Z is a constant multiplier, I is a constant increment, and M is a constant modulus. For example, suppose Z is 7, I is 5, and M is 12. If the first random number (usually called the seed) is 4, then we can determine the next few pseudo-random numbers are follows:


As you can see, the sequence of pseudo-random numbers generated by this technique repeats after six numbers. It should 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 values for Z, 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 the seed!

Input 
Each input line will contain four integer values, in order, for Z, I, M, and L. The last line will contain four zeroes, and marks the end of the input data. L will 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 sequence of pseudo-random numbers before the sequence is repeated.

Sample Input 
7 5 12 4
5173 3849 3279 1511
9111 5309 6000 1234
1079 2136 9999 1237
2054 4849 7436 3483
0 0 0 0

Sample Output 
Case 1: 6
Case 2: 546
Case 3: 500
Case 4: 220
Case 5: 10


这道题就是求循环长度,本身没什么,就是有个注意点,文中也提示了:But be careful: the cycle might not begin with the seed!意思就是循环不一定是回到开头,有可能从第i个数(比如是N)开始然后面到第j个数再次出现N,则循环长度是j-i。这个要注意,如果你没注意到,以为总是回到第一个数,就会发现第三个测试用例9111 5309 6000 1234输出的结果不对。下面贴出代码:

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int visit[100000];//记录出现过数字的次数

int main()
{
    int z,i,m,l;
    int j = 1;
    while(cin >> z >> i >> m >> l)
    {
        if(z == 0 && i == 0 && m == 0 && l == 0)
        {
            break;
        }
        int temp;
        memset(visit,0,sizeof(visit));
        vector<int> v;
        v.push_back(l);
        visit[l] = 1;
        int counter = 0;
        while(true)
        {
            l = (z*l+i)%m;
            ++visit[l];
            v.push_back(l);//把出现过的序列保存下来
            if(visit[l] == 2)//当一个数再次出现则说明出现了循环
            {
                temp = l;
                break;
            }
        }
        for(size_t i = v.size() -2; i >= 0; --i)
        {
            ++counter;
            if(v[i] == temp)//找到循环长度
            {
                break;
            }
        }
        cout << "Case " << j++ << ": " << counter<< endl;
    }
    return 0;
}

notice:尊重原创,转载或引用请注明出处


你可能感兴趣的:(C++,算法,OJ)