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

这一题到底是想干嘛的,简单的算法没过,反而暴力过了

Posted by hutu_2000 at 2009-03-14 13:35:35 on Problem 2381
没过的:
#include <map>
#include <iostream>
using namespace std;
int main()
{
	int a,c,m,r,max=0,d;
	map<int,bool> str; str.clear();
	scanf("%d %d %d %d",&a,&c,&m,&r);
	while(!str[r])
	{
		str[r]=true;
		r=(a*r+c)%m;
	}
	map<int,bool>::iterator i=str.begin(),j=str.end(),k;
	j--;
	while(i!=j)
	{
		k=i++;
		d=i->first - k->first;
		max=max>d?max:d;
	}
	printf("%d\n",max);
	return 0;
}

过了:
#include <iostream>
using namespace std;
int main()
{
	bool *flag;
	long R0, R, Rn, i, j, a, c, m, max, max_num;
	
	scanf("%ld%ld%ld%ld", &a, &c, &m, &R0);
	flag = new bool[m+1];
	memset(flag, false, sizeof(bool) * (m+1));
	flag[R0] = true;
	max = 0;
	max_num = 0;
	R = R0;
	for(i = 1; ; i++)
	{
		if(R > max_num)
			max_num = R;
		Rn = (a*R + c) % m;
		if(flag[Rn])
			break;
		else
			flag[Rn] = true;
		R = Rn;
	}
	for(i = 0; i < max_num; i++)
	{
		if(flag[i])
		{
			for(j = i+1; j <= max_num; j++)
			{
				if(flag[j])
					break;
			}
			if(max < j-i)
				max = j-i;		
			i = j-1;
		}
	}
	printf("%ld\n", max);
	
	return 1;
}

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