hdu 2824 The Euler function

来源:互联网 发布:python爬取天气数据 编辑:程序博客网 时间:2024/05/16 14:31

题目描述:求区间[a,b]内所有整数的欧拉函数的和,有多组数据


【分析】
裸的欧拉函数,参见百度百科 [传送门]


【代码】

//hdu 2824 The Euler function#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#define fo(i,j,k) for(int i=j;i<=k;i++)using namespace std;const int N=3000001;int m[N],p[N],phi[N];long long ans=0;inline void getphi(){    fo(i,2,N)    {        if(!m[i]) p[++p[0]]=m[i]=i,phi[i]=i-1;        for(int j=1;j<=p[0] && i*p[j]<N;j++)        {            m[i*p[j]]=p[j];            if(i%p[j]==0)            {                phi[i*p[j]]=phi[i]*p[j];                break;            }            else phi[i*p[j]]=phi[i]*(p[j]-1);        }    }}int main(){    int a,b;    while(scanf("%d%d",&a,&b)==2)    {        ans=0;        getphi();        fo(i,a,b)          ans+=phi[i];        cout<<ans<<endl;    }    return 0;}
1 0
原创粉丝点击