Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:考虑溢出了没有?

Posted by miaomiaomiaomiao at 2006-08-24 14:07:53 on Problem 2381
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator