单个欧拉函数

int eular(int n){
    int ret=1,i;
    for(i=2;i*i<=n;i++)
    if(n%i==0){
        n/=i,ret*=i-1;
        while(n%i==0)n/=i,ret*=i;
    }
    if(n>1) ret*=n-1;
    return ret;
}

筛法求欧拉函数

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=3000005;
long long phi[maxn];
int main(){
    int i,j,a,b;
    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);
    }
    while(scanf("%d%d",&a,&b)!=EOF){
        long long ans=0;
        for(i=a;i<=b;i++)ans+=phi[i];
        cout<<ans<<endl;
    }
    return 0;
}

相关文章:

  • 2022-12-23
  • 2021-10-09
  • 2022-02-15
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-11
  • 2021-11-25
相关资源
相似解决方案