最大公约数和

时间:2021-04-19 00:33:16

题面

所以给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)
N<=MAXINT

Sol

式子就是\(\sum_{d|N}d*\phi(N/d)\)
没了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e7 + 1);

IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
}

IL ll Phi(RG ll x){
RG ll cnt = x;
for(RG ll i = 2; i <= x; ++i){
if(x % i) continue;
while(x % i == 0) x /= i;
cnt -= cnt / i;
}
if(x > 1) cnt -= cnt / x;
return cnt;
}

int main(RG int argc, RG char *argv[]){
RG ll n = Read(), ans = 0, m = sqrt(n);
for(RG ll i = 1; i <= m; ++i){
if(n % i) continue;
ans += 1LL * i * Phi(n / i);
if(i * i != n) ans += 1LL * (n / i) * Phi(i);
}
printf("%lld\n", ans);
return 0;
}