hdu-2824

来源:互联网 发布:用excel数据分析方法 编辑:程序博客网 时间:2024/05/18 01:57

 (n) represents the amount of the numbers which are smaller than n and coprime to n, 

欧拉函数

φ函数的值  通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。 (注意:每种质因数只一个。比如12=2*2*3,那么φ(12)=12*(1-1/2)*(1-1/3)=4)
 
#include<iostream>#include<cstdio>#include<cmath>using namespace std;# define N 3000005_int64 phi[N];void init(){int i,j;for(i=1;i<N;++i)phi[i]=i;for(i=2;i<N;++i)if(phi[i]==i)//i为素数for(j=i;j<N;j+=i)phi[j]=(phi[j]/i)*(i-1);//j有因子i且i是素数}int main(){init();int a,b;while(scanf("%d%d",&a,&b)!=EOF){_int64 ans=0;for(int i=a;i<=b;++i)ans+=phi[i];printf("%I64d\n",ans);}return 0;}

  

 
原创粉丝点击