洛谷 P1390 公约数的和
题意:
给定$n$,求$\sum_{i=1}^{n}\sum_{j=i+1}^{n}\gcd(i, j)$
思路:
$\sum_{i=1}^{n}\sum_{j=i+1}^{n}\gcd(i, j)$
原式$=$
$\frac{1}{2} * \sum_{i=1}^{n}\sum_{j=1}^{n}\gcd(i, j) - \sum_{i=1}^{n}\gcd(i, i)$
即:
$\frac{1}{2} * \sum_{i=1}^{n}\sum_{j=1}^{n}\gcd(i, j) - \frac{n*(n+1)}{2}$
枚举最大公约数的值:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{i=1}^{n}\sum_{j=1}^{n}[\gcd(i, j)=d] - \frac{n*(n+1)}{2}$
简化式子:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}[\gcd(i, j)=1] - \frac{n*(n+1)}{2}$
将$[\gcd(i, j) = 1]$替换为$\varepsilon(\gcd(i, j))$:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}\varepsilon(\gcd(i, j)) - \frac{n*(n+1)}{2}$
展开$\varepsilon$函数得到:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{k|\gcd(i,j)} \mu(k) - \frac{n*(n+1)}{2}$
变换求和顺序,先枚举$k|\gcd(i, j)$得:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{k=1}\mu(k)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}[k|i]\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}[k|j] - \frac{n*(n+1)}{2}$
已知$1~\lfloor\frac{n}{d}\rfloor$中$k$的倍数有$\lfloor\frac{n}{kd}\rfloor$个:
$\frac{1}{2} * \sum_{d=1}^{n} d \sum_{k=1}\mu(k)\lfloor\frac{n}{kd}\rfloor\lfloor\frac{n}{kd}\rfloor - \frac{n*(n+1)}{2}$
Code:
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <map> #include <set> // #include <array> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> // #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define Time (double)clock() / CLOCKS_PER_SEC #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 2e6 + 20; const int M = 1e5 + 20; const int mod = 1e9 + 7; const double eps = 1e-6; int cnt = 0, primes[N], mu[N], sum[N]; bool st[N]; void get(int n){ mu[1] = 1; for(int i = 2; i <= n; i ++){ if(!st[i]){ primes[cnt ++] = i; mu[i] = -1; } for(int j = 0; primes[j] <= n / i; j ++){ st[i * primes[j]] = true; if(i % primes[j] == 0){ mu[i * primes[j]] = 0; break; } mu[i * primes[j]] = -mu[i]; } } sum[0] = 0; for(int i = 1; i <= n; i ++){ sum[i] = sum[i - 1] + mu[i]; } } void solve() { int n; sd(n); ll ans = 0; for(int d = 1; d <= n; d ++){ ll res = 0; int m = n / d; for(int l = 1, r; l <= m; l = r + 1){ r = m / (m / l); res = res + (ll)(sum[r] - sum[l - 1]) * (m / l) * (m / l); } ans = ans + res * d; } ans = ans - (ll)n * (n + 1) / 2; ans /= 2; printf("%lld\n", ans); } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/code/out.txt", "w", stdout); // freopen("/home/jungu/code/practice/out.txt","w",stdout); #endif // ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; // sd(T); get(2000000); // init(); // int cas = 1; while (T--) { // printf("Case #%d:", cas++); solve(); } return 0; }