[BZOJ2705][SDOI2012]Longge的问题

[SDOI2012]Longge的问题

Description
Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题。现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。
Input
一个整数,为N。
Output
一个整数,为所求的答案。
Sample Input
6
Sample Output
15
HINT
【数据范围】
对于60%的数据,0 < N<=2^16。
对于100%的数据,0 < N<=2^32。

Solution

i=1ngcd(i,n)

=d|ndi=1n[gcd(i,n)==d]

=d|ndi=1nd[gcd(i,nd)==1]

=d|ndϕ(nd)

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
    x=0;T f=1;char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    x*=f;
}

const int MaxN=100000;
ll n,tot,d[MaxN],ans=0;

inline ll phi(ll n){
    ll res=n;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){res/=i;res*=(i-1);}
        while(n%i==0) n/=i;
    }
    if (n!=1) res/=n,res*=(n-1);
    return res;
}
int main(){
    read(n);
    for(ll i=1;i*i<=n;i++){
        if(n%i==0) d[++tot]=i,d[++tot]=n/i;
        if(i*i==n) tot--;
    }
    for(int i=1;i<=tot;i++) ans+=d[i]*phi(n/d[i]);
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值