题意:
给n,求1-n gcd(i,n) 的和。
解析:
gcd(i, m * n) = gcd(i * m) * gcd(i * n),m,n互质,所以gcd是积性函数,所以和函数也是积性函数。
令f(n) = sigma gcd(i,n)。可知f(n) = sum(p * phi(n / p)),p为n的因子。
由此:
f(p^r) = r * (p^r - p ^(r - 1)) + p ^ r.
把n素因子分解,带公式就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
using namespace std;
LL solve(LL n)
{
LL res = 1;
for (LL i = 2; i * i <= n; i++)
{
LL t = 1;
LL r = 0;
while (n % i == 0)
{
n /= i;
t *= i;
r++;
}
res *= (r + 1) * t - t / i * r;
}
if (n != 1)
res *= 2 * n - 1;
return res;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
LL n;
while (~scanf("%lld", &n))
{
printf("%lld\n", solve(n));
}
return 0;
}