HDU-2824 The Euler function(欧拉函数)

The Euler function

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


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
 
Source
 
Recommend
 
欧拉函数挺有用的似乎,欧拉函数表和朴素筛法是有着异曲同工之妙的
 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX=3e6+5;
 5 int phi[MAX];
 6 void eular(){
 7     int i,j;
 8     for (i=1;i<MAX;i++) phi[i]=i;
 9     for (i=2;i<MAX;i+=2) phi[i]/=2;
10     for (i=3;i<MAX;i+=2){
11         if (phi[i]==i)
12             for (j=i;j<MAX;j+=i)
13                 phi[j]=phi[j]/i*(i-1);
14     }
15 }
16 int main(){
17     freopen ("euler.in","r",stdin);
18     freopen ("euler.out","w",stdout);
19     int i,j;
20     eular();
21     LL x,y,ans;
22     while (~scanf("%lld%lld",&x,&y)){
23         for (i=x,ans=0;i<=y;i++) ans+=(LL)phi[i];
24         printf("%lld
",ans);
25     }
26     return 0;
27 }
未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
原文地址:https://www.cnblogs.com/keximeiruguo/p/7664005.html