题解 luoguP2303 【[SDOi2012]Longge的问题】

好水的蓝题啊,少数没有看题解做的题

题面简洁明了:求 ∑ i = 1 n g c d ( i , n ) \sum\limits_{i=1}^{n}gcd(i,n) i=1ngcd(i,n)

g c d ( i , n ) = d gcd(i,n)=d gcd(i,n)=d,则 g c d ( i / d , n / d ) = 1 gcd(i/d,n/d)=1 gcd(i/d,n/d)=1

d d d显然就是 n n n的因数

我们对于每个 d d d,要求有多少 i i i使得 g c d ( i / d , n / d ) = 1 gcd(i/d,n/d)=1 gcd(i/d,n/d)=1,设求出有 x x x i i i,那么对答案的贡献就是 d × x d \times x d×x。为什么是这些贡献?很显然,这些 i i i n n n g c d gcd gcd就是 d d d,共 x x x个这样的 i i i,所以是 d × x d \times x d×x

因为满足$gcd(i/d,n/d)= 1 的 1的 1i/d 的 个 数 就 是 与 的个数就是与 n/d 互 质 的 数 , 每 个 互质的数,每个 i/d 又 对 应 一 个 又对应一个 i , 个 数 就 是 ,个数就是 φ(n/d) 。 前 面 说 了 。前面说了 d 是 是 n$的因数,我们枚举所有因数,累加答案即可。

对于每个 φ ( n / d ) φ(n/d) φ(n/d) n \sqrt{n} n 求即可。

代码:

#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define oo (1e18)
#define int long long
#define LL unsigned long long
#define hh puts("")
using namespace std;
int ans,n;
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=(ret<<3)+(ret<<1)+(ch^48);ch=getchar();}
    return ret*ff;
}
inline int getphi(int x){
    int res=x;
    for(int i=2;i*i<=x;i++){
        if(x%i==0){
            res=res/i*(i-1);
            while(x%i==0) x/=i; 
        }
    }
    if(x>1) res=res/x*(x-1);
    return res;
}
signed main(){
    n=read();
    int sq=sqrt(n);
    for(int i=1;i<=sq;i++){
        if(n%i==0){
            ans+=getphi(n/i)*i;
            if(i*i!=n) ans+=getphi(i)*(n/i);
        }
    }
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值