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 glacier at 2006-04-18 17:32:06 on Problem 2773
#include<iostream>
using namespace std;
#include<math.h>
long GCD(long a,long b)//a,b的最大公约数
{
	long c;
	if(a>b)
	{
		c=a%b;
		while(c!=0)
		{
			a=b;
			b=c;
			c=a%b;
		}
		return b;
	}
	else
	{
		c=b%a;
		while(c!=0)
		{
			b=a;
			a=c;
			c=b%a;
		}
		return a;
	}
}
long f(long m)//m以内与它互素的数的个数
{
	long num(0);
	long d[20]={0};//用来存放m的约数
	int n(0);
	long m1=m;
	for(long i=2;i<=m;i++)
	{
		if(m%i==0)
		{
			while(m%i==0)
			{
				m=m/i;	
			}
			d[n++]=i;
		}
	}
	
	for(i=0;i<n;i++)
		m1=m1*(d[i]-1)/d[i];
	return m1;

}

void main()
{
	long m,k;
	long l,r;
	long b;
	long n=0;//与m互素的数的个数

	while(true)
	{
		cin>>m>>k;
		n=0;
		if(f(m)>k)
		{
			for(long j=1;n!=k;j++)
			{
				if(GCD(j,m)==1)
				{
					n++;
				}
			}
			cout<<j-1<<endl;
		}
		else
		{
			b=2;
			while(b*f(m)<k)
			{
				b=b*2;
			}
			l=0;
			r=b;
			while(l<b)
			{
				if(b*f(m)>k)
				{
					r=b;
					b=(l+r)/2;
				}
				else
				{
					l=b;
					b=(l+r)/2;
				}
			}

			
			n=l*f(m);
			for(long j=l*m;n!=k;j++)
			{
				if(GCD(j,m)==1)
				{
					n++;
				}
			}
			cout<<j-1<<endl;
		}
	}

	
}


						













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