杭电 2824 筛法求欧拉函数的和

    此题可以用筛法求欧拉函数,其核心思想就是不断的去除某个数的质因数,和普通方法求欧拉函数是一样的,只不过这是把求欧拉函数的过程写在了筛法的过程中。提高了效率。题目:

The Euler function

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


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
 

ac代码:

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
#define M 3000010
_int64 num[M];
void prime(int x){
  for(int i=2;i<=x;++i)
	  num[i]=i;
  for(int i=2;i<=x;++i){
	  if(num[i]==i){
	    for(int j=i;j<=x;j+=i)
			num[j]=(num[j]*(i-1))/i;
	  }
  }
}
int main(){
  int a,b;
  prime(M);
  while(~scanf("%d%d",&a,&b)){
	_int64 sum=0;
	for(int i=a;i<=b;++i)
		sum+=num[i];
	printf("%I64d\n",sum);
  }
  return 0;
}


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