题目简述
给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。
题解
令t(x)为 gcd(i,N) 为 x 的个数。∴ t(1)=Φ(N) ;
t(x)=∑i<=ni=1(gcd(i,N)==x)=∑i<=ni=1(gcd(ix,nx)==1)=∑Φ(nx)
t
(
x
)
=
∑
i
<=
n
i
=
1
(
g
c
d
(
i
,
N
)
==
x
)
=
∑
i
<=
n
i
=
1
(
g
c
d
(
i
x
,
n
x
)
==
1
)
=
∑
Φ
(
n
x
)
故当x是n的因数时求出Φ(x/n)即可
ans=Σ(x∗Φ(n/x))
a
n
s
=
Σ
(
x
∗
Φ
(
n
/
x
)
)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
ll n;
inline ll phi(ll x)
{
if(x==1) return 1;
ll res=x;
for(register ll i=2;i<=sqrt(x);i++){
if(x%i==0){
res-=res/i;
while(x%i==0) x/=i;
}
}
if(x!=1) res-=res/x;
return res;
}
int main()
{
scanf("%lld",&n);
register ll ans=0;
for(register ll i=1;i<=sqrt(n);i++){
if(n%i==0){
ans+=i*phi(n/i);
if(i!=n/i) ans+=(n/i)*phi(i);
}
}
printf("%lld\n",ans);
}