1238 最小公倍数之和 V3
推式子
∑i=1n∑j=1nlcm(i,j)=∑i=1n∑j=1nijgcd(i,j)=∑d=1n∑i=1n∑j=1nijd(gcd(i,j)==d)=∑d=1nd∑i=1nd∑j=1ndij(gcd(i,j)==1)=∑d=1nd∑i=1nd∑j=1ndij∑k∣gcd(i,j)μ(k)=∑d=1nd∑k=1ndk2μ(k)∑i=1ndk∑j=1ndkij\sum_{i = 1} ^{n} \sum_{j = 1} ^{n} lcm(i, j)\\ = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}\frac{ij}{gcd(i, j)}\\ =\sum_{d = 1} ^{n} \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \frac{ij}{d} (gcd(i, j) == d)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}ij(gcd(i, j) == 1)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}ij\sum_{k \mid gcd(i, j)} \mu(k)\\ = \sum_{d = 1} ^{n} d \sum_{k = 1} ^{\frac{n}{d}} k ^ 2\mu(k)\sum_{i = 1} ^{\frac{n}{dk}} \sum_{j = 1} ^{\frac{n}{dk}}ij\\ i=1∑nj=1∑nlcm(i,j)=i=1∑nj=1∑ngcd(i,j)ij=d=1∑ni=1∑nj=1∑ndij(gcd(i,j)==d)=d=1∑ndi=1∑dnj=1∑dnij(gcd(i,j)==1)=d=1∑ndi=1∑dnj=1∑dnijk∣gcd(i,j)∑μ(k)=d=1∑ndk=1∑dnk2μ(k)i=1∑dknj=1∑dknij
化简到这里好像能通过两次数论分块得到我们的答案,但是复杂度是O(n)O(n)O(n)的,显然不行,所以我们考虑另一种化简方法。
∑i=1n∑j=1nlcm(i,j)=∑i=1n∑j=1nijgcd(i,j)=∑d=1n∑i=1n∑j=1nijd(gcd(i,j)==d)=∑d=1nd∑i=1nd∑j=1ndij(gcd(i,j)==1)这里两个的上届都是nd,所以我们可以分类讨论一下,i>j,i==j,i<j,由于i>j,i<j可以合并,所以上式变成=∑d=1nd(2∑i=1ndi(∑j=1ij×(gcd(i,j)==1))−1)根据欧拉函数定理,我们可以再次化简=∑d=1nd(2∑i=1ndi(iϕ(i)+(i==1)2)−1)=∑d=1nd∑i=1ndi2ϕ(i)接下来就是按照套路用杜教筛求解∑i=1ndi2ϕ(i)了S(n)=∑i=1ni2ϕ(i)=∑i=1nf(i)∑i=1n(f∗g)(i)=∑i=1ng(i)S(ni)g(1)S(n)=∑i=1n(f∗g)(i)−∑d=2ng(i)S(nd)(f∗g)(i)=∑d∣if(d)g(id)=∑d∣id2ϕ(d)g(id)容易想到∑d∣iϕ(d)=i,所以如果可以提出d2那就完美了,我们可以另g(i)=i2,上式变成∑d∣id2ϕ(d)i2d2=i2∑d∣iϕ(d)=i3S(n)=∑i=1ni3−∑d=1nd2S(nd)=n2(n+1)24−∑d=1nd2S(nd)\sum_{i = 1} ^{n} \sum_{j = 1} ^{n} lcm(i, j)\\ = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}\frac{ij}{gcd(i, j)}\\ =\sum_{d = 1} ^{n} \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \frac{ij}{d} (gcd(i, j) == d)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}ij(gcd(i, j) == 1)\\ 这里两个的上届都是\frac{n}{d},所以我们可以分类讨论一下,i > j, i == j, i < j,由于i > j, i < j可以合并,所以上式变成\\ = \sum_{d = 1} ^{n} d (2\sum_{i = 1} ^{\frac{n}{d}} i (\sum_{j = 1} ^{i}j\times (gcd(i, j) == 1)) - 1)\\ 根据欧拉函数定理,我们可以再次化简\\ = \sum_{d = 1} ^{n} d (2\sum_{i = 1} ^{\frac{n}{d}} i (\frac{i\phi(i) + (i == 1)}{2}) - 1)\\ = \sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}} i ^ 2 \phi(i)\\ 接下来就是按照套路用杜教筛求解\sum_{i = 1} ^{\frac{n}{d}} i ^ 2 \phi(i)了\\ S(n) = \sum_{i = 1} ^{n} i ^ 2 \phi(i) = \sum_{i = 1} ^{n} f(i) \\ \sum_{i = 1} ^{n} (f * g)(i) \\ = \sum_{i = 1} ^{n}g(i)S(\frac{n}{i})\\ g(1)S(n) = \sum_{i = 1} ^{n} (f * g)(i) - \sum_{d = 2} ^{n} g(i)S(\frac{n}{d})\\ (f * g)(i) = \sum_{d \mid i} f(d)g(\frac{i}{d}) = \sum_{d \mid i} d ^ 2 \phi(d)g(\frac{i}{d})\\ 容易想到\sum_{d \mid i} \phi(d) = i, 所以如果可以提出d ^ 2那就完美了,我们可以另g(i) = i ^ 2,上式变成\\ \sum_{d \mid i} d ^ 2 \phi(d) \frac{i ^ 2}{d ^ 2} = i ^ 2 \sum_{d \mid i} \phi(d) = i ^ 3 \\S(n) = \sum_{i = 1} ^{n} i ^ 3 - \sum_{d = 1} ^{n} d ^ 2S(\frac{n}{d})\\ = \frac{n ^ 2 (n + 1) ^ 2}{4} - \sum_{d = 1} ^{n} d ^ 2S(\frac{n}{d})\\ i=1∑nj=1∑nlcm(i,j)=i=1∑nj=1∑ngcd(i,j)ij=d=1∑ni=1∑nj=1∑ndij(gcd(i,j)==d)=d=1∑ndi=1∑dnj=1∑dnij(gcd(i,j)==1)这里两个的上届都是dn,所以我们可以分类讨论一下,i>j,i==j,i<j,由于i>j,i<j可以合并,所以上式变成=d=1∑nd(2i=1∑dni(j=1∑ij×(gcd(i,j)==1))−1)根据欧拉函数定理,我们可以再次化简=d=1∑nd(2i=1∑dni(2iϕ(i)+(i==1))−1)=d=1∑ndi=1∑dni2ϕ(i)接下来就是按照套路用杜教筛求解i=1∑dni2ϕ(i)了S(n)=i=1∑ni2ϕ(i)=i=1∑nf(i)i=1∑n(f∗g)(i)=i=1∑ng(i)S(in)g(1)S(n)=i=1∑n(f∗g)(i)−d=2∑ng(i)S(dn)(f∗g)(i)=d∣i∑f(d)g(di)=d∣i∑d2ϕ(d)g(di)容易想到d∣i∑ϕ(d)=i,所以如果可以提出d2那就完美了,我们可以另g(i)=i2,上式变成d∣i∑d2ϕ(d)d2i2=i2d∣i∑ϕ(d)=i3S(n)=i=1∑ni3−d=1∑nd2S(dn)=4n2(n+1)2−d=1∑nd2S(dn)
最终化简的式子∑d=1ndS(nd)\sum_{d = 1} ^{n} dS(\frac{n}{d})∑d=1ndS(dn)
代码
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}const int N = 8e6 + 10, mod = 1000000007;ll phi[N], n, inv4, inv6, inv2;int prime[N], cnt;bool st[N];ll quick_pow(ll a, ll n, ll mod) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}void init() {phi[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[cnt++] = i;phi[i] = i - 1;}for(int j = 0; 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);}}for(int i = 1; i < N; i++) {phi[i] = (phi[i - 1] + 1ll * i * i % mod * phi[i] % mod) % mod;}inv2 = quick_pow(2, mod - 2, mod), inv4 = quick_pow(4, mod - 2, mod), inv6 = quick_pow(6, mod - 2, mod);
}ll calc1(ll x) {x %= mod;return 1ll * x * x % mod * (x + 1) % mod * (x + 1) % mod * inv4 % mod;
}ll calc2(ll x) {x %= mod;return x * (x + 1) % mod * (2ll * x + 1) % mod * inv6 % mod;
}unordered_map<ll, ll> ans_phi;ll get_phi(ll x) {if(x < N) return phi[x];if(ans_phi.count(x)) return ans_phi[x];ll ans = calc1(x);for(ll l = 2, r; l <= x; l = r + 1) {r = x / (x / l);ans -= (calc2(r) - calc2(l - 1)) * get_phi(x / l) % mod;ans = (ans % mod + mod) % mod;}return ans_phi[x] = ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);ll n = read();init();ll ans = 0;for(ll l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans += (l + r) % mod * (r - l + 1) % mod * inv2 % mod * get_phi(n / l) % mod;ans %= mod; }printf("%lld\n", ans);return 0;
}