| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:考虑溢出了没有?In Reply To:考虑溢出了没有? Posted by:041221125 at 2006-08-23 21:52:04 我是找的循环节 思路对吗??标记一个pre[],一个back[],每次都把r1 r2和pre和back数组中找如果找到说明已经出现了一个循环,跳出即可,中间过程中已经找到了最大的差值了。对吗???
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned pre[160000001];
unsigned back[160000001];
unsigned r1,r2;
unsigned m,a,c,i;
unsigned tail;
unsigned maxdif;
bool flag;
int main(int argc, char *argv[])
{
while(scanf("%d %d %d %d",&a,&c,&m,&r1)!=EOF)
{
flag=0;
tail=0;
r2=(a*r1+c)%m;
pre[tail]=r1;
back[tail]=r2;
tail++;
maxdif=abs(r2-r1);
if(r2==r1)
{
printf("0\n");
continue;
}
while(1)
{
r1=r2;
r2=(a*r1+c)%m;
if(abs(r2-r1)>maxdif)
maxdif=abs(r2-r1);
for(i=0;i<tail;i++)
{
if(pre[i]==r1&&back[i]==r2)
{
flag=1;
break;
}
}
if(flag)
break;
pre[tail]=r1;
back[tail]=r2;
tail++;
}
printf("%d\n",maxdif);
}
return EXIT_SUCCESS;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator