HDU 3501【欧拉函数拓展求一个数的所有质因子之和】

来源:互联网 发布:大圣数据 编辑:程序博客网 时间:2024/05/16 15:38

欧拉函数

欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 。
通式:
这里写图片描述
其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。
对于质数p,φ(p) = p - 1。注意φ(1)=1.
欧拉定理:对于互质的正整数a和n,这里写图片描述
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。
若n是质数p的k次幂这里写图片描述,因为除了p的倍数外,其他数都跟n互质。
特殊性质:当n为奇数时,φ(2n)=φ(n)

欧拉函数还有这样的性质:
设a为N的质因数,
若(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)。

欧拉公式的延伸:一个数的所有质因子之和是euler(n)*n/2。

#include <bits/stdc++.h>using namespace std;typedef long long LL;const LL mod=1e9+7;LL eluer(LL n){    LL res=n,a=n;    for(LL i=2;i*i<=a;i++)    {        if(a%i==0)        {            res=res/i*(i-1);            while(a%i==0)                a/=i;        }    }    if(a>1) res=res/a*(a-1);    return res;}int main(){    LL n,ans;    while(~scanf("%lld",&n)&&n)    {        ans=n*(n+1)/2-n;        ans=(ans-eluer(n)*n/2)%mod;        printf("%lld\n",ans);    }    return 0;}
0 0