poj 2480 Longge's problem(积性函数 & 欧拉函数)

http://poj.org/problem?id=2480

大意:求解 ∑gcd(i, N) 1<=i <=N。

对于最大公约数,它有这样的性质,gcd(n,m1*m2)=gcd(nm1)*gcd(n,m2)   比如gcd(6,12)=gcd(6,3)*gcd(6,4).

我们设f(N)=∑gcd(i, N),那么它也应该是一个积性函数。(积性函数的积是积性函数,积性函数的和是积性函数)

其中p是和n互素的数字。

又设 ,由积性函数的性质:

      (*)

同时我们知道:

 欧拉函数有这样的计算方式:

所以得到:


带入(*)中:


写代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;

int main()
{
    LL n,ans;
    while(cin>>n){
        int len=sqrt(n*1.0);
        ans=n;
        LL a=0,p=1;
        for(int i=2;i<=len;i++){
            a=0;
            if(n%i==0){
               a++;
               n/=i;
               p=i;
               while(n%i==0) {
                   a++;
                   n/=i;
               }
               ans=ans/p*(p+a*p-a);
            }
        }
        if(n>1){
            ans=ans/n*(n+n-1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值