【杭电2824】欧拉函数

    xiaoxiao2021-03-25  127

    F - F Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status

    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 解析:(转)

    定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目。

        例如:φ(8)=4,因为1357均和8互质。

    性质:1.p是质数,φ(p)= p-1.

       2.n是质数pk次幂,φ(n)=(p-1)*p^(k-1)。因为除了p的倍数都与n互质

       3.欧拉函数是积性函数,若m,n互质,φ(mn)= φ(m)φ(n).

      根据这3条性质我们就可以推出一个整数的欧拉函数的公式。因为一个数总可以写成一些质数的乘积的形式。

      E(k)=(p1-1)(p2-1)...(pi-1)*(p1^(a1-1))(p2^(a2-1))...(pi^(ai-1))

        = k*(p1-1)(p2-1)...(pi-1)/(p1*p2*...*pi)

        = k*(1-1/p1)*(1-1/p2)...(1-1/pk)

    在程序中利用欧拉函数如下性质,可以快速求出欧拉函数的值(aN的质因素)

      若( N%a ==0&&(N/a)%a ==0)则有:E(N)= E(N/a)*a;

      若( N%a ==0&&(N/a)%a !=0)则有:E(N)= E(N/a)*(a-1);

    code:

    #include<cstdio> #include<algorithm> using namespace std; const int N=3000001; int prime[N],isprime[N],num[N]; void oula() { int t=0; for(int i=2;i<N;i++){ if(isprime[i]==0){//i是质数 prime[t++]=i; //记录 num[i]=i-1;//欧拉函数性质 } for(int j=0;j<t&&i*prime[j]<N;j++){ isprime[i*prime[j]]=1;//i*prime[j]相当于n,prime[j]是n的质因子 if(i%prime[j]==0) num[i*prime[j]]=num[i]*prime[j]; else num[i*prime[j]]=num[i]*(prime[j]-1); } } } int main() { int a,b; oula(); while(~scanf("%d%d",&a,&b)){ long long sum=0; for(int i=a;i<=b;i++) sum+=num[i]; printf("%lld\n",sum); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-6703.html

    最新回复(0)