UVa 350 - Pseudo-Random Numbers

// Accepted

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>

#define sf scanf
#define pf printf

using namespace std;

typedef map<int , bool>mpbi;

int main()
{
    int z, i, m, l , ans=0 , tc=0;

    mpbi mp;

    while(~sf("%d %d %d %d" , &z, &i, &m, &l))
    {
        mp.clear();

        if(!z and !i and !m and !l) break;

        int cnt=0;

        ans = ((z * l) + i) % m;

        while(!mp[ans])
        {
            cnt++;

            mp[ans] = true;

            ans = ((z * ans) + i) % m;
        }

        pf("Case %d: %d\n", ++tc, cnt);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number