HDOJ 2824 The Euler function(欧拉函数+打表法)

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4516    Accepted Submission(s): 1878


Problem Description
The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
 

Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
 

Output
Output the result of (a)+ (a+1)+....+ (b)
 

Sample Input
   
   
   
   
3 100
 

Sample Output
   
   
   
   
3042
 
 
题意:给出a,b两个数。(n)表示小于或者等于n的正整数中与n互质的个数。求(a)+(a+1)+.....+(b)的值。
 
很明显是欧拉函数问题,a,b值的上限很大,所以必须打表。不懂打表代码,只有死记模板了。
 
欧拉函数打表,代码如下:
 
 
<span style="font-size:12px;">#include<cstdio>
#include<cstring>
#define maxn 3000010
__int64 res[maxn];

__int64 euler()
{
	int i,j;
	memset(res,0,sizeof(res));
	res[1]=1;
	for(i=2;i<maxn;++i)//先打表出每个数的互质数个数 
	{
		if(!res[i])
		{
			for(j=i;j<maxn;j+=i)
			{
				if(!res[j])
				res[j]=j;
				res[j]=res[j]/i*(i-1);
			}
		}
	}
	for(i=2;i<maxn;++i)//再循环相加,找出从1到这个数的互质数和 
	   res[i]+=res[i-1];
}

int main()
{
	euler();
	int a,b;
	while(scanf("%d%d",&a,&b)!=EOF)
	   printf("%I64d\n",res[b]-res[a-1]);
	return 0;
}
</span>

 
 

你可能感兴趣的:(HDOJ 2824 The Euler function(欧拉函数+打表法))