Poj Solution 2381

http://poj.org/problem?id=2381

#include <stdio.h>
#include <string.h>


char b[16000002];
int main()
{
    unsigned long a,m,c,r;
    memset(b,0,sizeof(b));
    scanf("%lu%lu%lu%lu",&a,&c,&m,&r);
    while(1)
    {
        r=(a*r+c)%m;
        if(b[r])
            break;
        else b[r]=1;
    }
    long i,max=0,last=0;
    for(i=0;i<16000002;i++)
        if(b[i])
            break;
    last=i++;
    for(;i<16000002;i++)
    {
        if(b[i])
        {
//            printf("%lu ",i);
            if(i-last>max)
            {
                max=i-last;
            }
            last=i;
        }
    }
    printf("%lu",max);
    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *