A Simple Math Problem
∑i=1n∑j=1if(j)[gcd(i,j)=1]∑i=1n∑j=inf(i)[gcd(i,j)=1]∑i=1n∑j=1nf(i)[gcd(i,j)=1]−∑i=1n∑j=1if(i)[gcd(i,j)=1]+f(1)∑d=1nμ(d)nd∑i=1ndf(id)−∑i=1nf(i)ϕ(i)+f(1)然后就可以O(nlogn)求解了\sum_{i = 1} ^{n} \sum_{j = 1} ^{i} f(j)[gcd(i, j) = 1]\\ \sum_{i = 1} ^{n} \sum_{j = i} ^{n} f(i)[gcd(i, j) = 1]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} f(i)[gcd(i, j) = 1] - \sum_{i = 1} ^{n} \sum_{j = 1} ^{i}f(i)[gcd(i, j) = 1] + f(1)\\ \sum_{d = 1} ^{n} \mu(d) \frac{n}{d}\sum_{i = 1} ^{\frac{n}{d}} f(id) - \sum_{i = 1} ^{n}f(i) \phi(i) + f(1)\\ 然后就可以O(n \log n)求解了\\ i=1∑nj=1∑if(j)[gcd(i,j)=1]i=1∑nj=i∑nf(i)[gcd(i,j)=1]i=1∑nj=1∑nf(i)[gcd(i,j)=1]−i=1∑nj=1∑if(i)[gcd(i,j)=1]+f(1)d=1∑nμ(d)dni=1∑dnf(id)−i=1∑nf(i)ϕ(i)+f(1)然后就可以O(nlogn)求解了
/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10;ll prime[N], phi[N], mu[N], f[N], g[N], cnt, n;bool st[N];void init() {phi[1] = mu[1] = f[1] = 1;for(int i = 2; i <= n; i++) {f[i] = f[i / 10] + i % 10;if(!st[i]) {prime[++cnt] = i;phi[i] = i - 1;mu[i] = -1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] <= n; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {phi[i * prime[j]] = phi[i] * prime[j];break;}phi[i * prime[j]] = phi[i] * (prime[j] - 1);mu[i * prime[j]] = -mu[i];}}for(int i = 1; i <= n; i++) {for(int j = i; j <= n; j += i) {g[i] += f[j];}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%lld", &n);init();ll ans1 = 0, ans2 = 0;for(int i = 1; i <= n; i++) {ans1 += mu[i] * (n / i) * g[i];ans2 += f[i] * phi[i];}printf("%lld\n", ans1 - ans2 + 1);return 0;
}