The Euler function

﹏ヽ暗。殇╰゛Y 2022-03-18 02:50 258阅读 0赞

题目描述:

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

思路:就是求一个区间内的欧拉函数之和,

给出一个结论:

设E( x)为X的欧拉函数,p为质数

对于E(x*p)

if p是x的因数 E(x*p)=E(x)*p;

else E (x*P)=E(x)*(p-1);

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxv=3e6+10;
  4. int v[3000005],a[3000006],b[3000005];//b数组就是欧拉函数
  5. void init(){
  6. for(int i=2;i<3000006;i++){
  7. if(!v[i]){
  8. a[++a[0]]=i;
  9. b[i]=i-1;
  10. }
  11. for(int j=1;j<=a[0]&&i*a[j]<3000006;j++){
  12. v[i*a[j]]=1;
  13. if(i%a[j]==0){
  14. b[i*a[j]]=b[i]*a[j];
  15. break;
  16. }else{
  17. b[i*a[j]]=b[i]*(a[j]-1);
  18. }
  19. }
  20. }
  21. }
  22. int main(){
  23. int n,m,len;
  24. init();
  25. long long sum=0;
  26. cin>>n>>m;
  27. if(n>m)swap(n,m);
  28. for(int i=n;i<=m;i++)sum+=b[i];
  29. cout<<sum;
  30. }

发表评论

表情:
评论列表 (有 0 条评论,258人围观)

还没有评论,来说两句吧...

相关阅读