hdu2824 The Euler function 欧拉函数模板题

The Euler function

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


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
#include<stdio.h>
#include<math.h>
#define maxn 3000000
int phi[3000000+100];
void get_PHI()
{
    int i,j;
	for (i = 1; i <= maxn; i++) phi[i] = i;
	for (i = 2; i <= maxn; i += 2) phi[i] /= 2;
	for (i = 3; i <= maxn; i += 2) if(phi[i] == i)
	{
		for (j = i; j <= maxn; j += i)
			phi[j] = phi[j] / i * (i - 1);
	}
}
	int main()
	{
		int a,b,i;
		get_PHI();
		while(scanf("%d%d",&a,&b)!=EOF)
		{
			__int64 sum=0;
			for(i=a;i<=b;i++)
				sum+=phi[i];
			printf("%I64d\n",sum);
		}
		return 0;
	}

//下面是爆内存的  但是有一点错误 值得 反省   所以也贴上

#include<stdio.h>
#include<math.h>
#define maxn 3000000
int phi[3000000+100];
__int64 sum[3000000+100];
void get_PHI()
{
    int i,j;
	for (i = 1; i <= maxn; i++) phi[i] = i;
	for (i = 2; i <= maxn; i += 2) phi[i] /= 2;
	for (i = 3; i <= maxn; i += 2) if(phi[i] == i)
	{
		for (j = i; j <= maxn; j += i)
			phi[j] = phi[j] / i * (i - 1);
	}
}
	
	int main()
	{
		int a,b,i;
		get_PHI();
		sum[2]=phi[2];
           for(i=3;i<=3000000;i++)
			    sum[i]=sum[i-1]+phi[i];
		while(scanf("%d%d",&a,&b)!=EOF)
		{
			printf("%I64d\n",sum[b]-sum[a-1]);//注意这里 是a-1 以前也遇见过类似的情况 
			/*对于一串数相加后的sum 要特别注意下标应该是什么 千万特别留意下*/
		}
		return 0;
	}


你可能感兴趣的:(function,BI,input,each,output,Numbers)