2013年10月10日 星期四

UVa 350 - Pseudo-Random Numbers

Problem link

Keyword: ad hoc, search

Algorithm:

Simulate the process and check if a certain number already exists.
I used a O(1) method to check the existence.
Then, find out the length.

Code:
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
    int seed, incre, times, mod, t = 0;

    while( scanf( "%d %d %d %d", &times, &incre, &mod, &seed ) && mod )
    {
        int seq[ 10000 ] = { seed }, size = 1;
        bool has[ 10000 ] = { false };
        has[ seed ] = true;
        for( seed = ( seed * times + incre ) % mod; !has[ seed ]; seed = ( seed * times + incre ) % mod )
            seq[ size++ ] = seed, has[ seed ] = true;
        printf( "Case %d: %d\n", ++t, seq + size - find( seq, seq+size, seed ) );
    }
    return 0;
}

沒有留言:

張貼留言