1237 最大公约数之和 V3
推式子
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n ( g c d ( i , j ) = = d ) = ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d ( g c d ( i , j ) = = 1 ) = ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d ∑ k ∣ g c d ( i , j ) μ ( k ) = ∑ d = 1 n d ∑ k = 1 n d μ ( k ) ∑ i = 1 n k d ∑ j = 1 n k d 1 套 路 地 设 t = k d = ∑ t = 1 n ( ⌊ n t ⌋ ) 2 ∑ d ∣ t d μ ( t d ) = ∑ t = 1 n ( ⌊ n t ⌋ ) 2 ϕ ( t ) 接 下 来 就 是 杜 教 筛 求 ∑ i = 1 n ϕ ( i ) 了 , 那 这 不 就 是 杜 教 筛 水 题 了 嘛 。 \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} gcd(i, j)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^ {n} \sum_{j = 1} ^ {n} (gcd(i, j) == d)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}(gcd(i, j) == 1)\\ = \sum_{d = 1} ^{n} d\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}} \sum_{k \mid gcd(i, j)} \mu(k)\\ = \sum_{d = 1} ^{n} d\sum_{k = 1} ^{\frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{n}{kd}} \sum_{j = 1} ^{\frac{n}{kd}}1\\ 套路地设t = kd\\ =\sum_{t = 1} ^{n} \left(\lfloor\frac{n}{t}\rfloor \right) ^ 2 \sum_{d \mid t} d \mu(\frac{t}{d})\\ =\sum_{t = 1} ^{n} \left(\lfloor\frac{n}{t}\rfloor \right) ^ 2 \phi(t)\\ 接下来就是杜教筛求\sum_{i = 1} ^{n} \phi(i)了,那这不就是杜教筛水题了嘛。 i=1∑nj=1∑ngcd(i,j)=d=1∑ndi=1∑nj=1∑n(gcd(i,j)==d)=d=1∑ndi=1∑dnj=1∑dn(gcd(i,j)==1)=d=1∑ndi=1∑dnj=1∑dnk∣gcd(i,j)∑μ(k)=d=1∑ndk=1∑dnμ(k)i=1∑kdnj=1∑kdn1套路地设t=kd=t=1∑n(⌊tn⌋)2d∣t∑dμ(dt)=t=1∑n(⌊tn⌋)2ϕ(t)接下来就是杜教筛求i=1∑nϕ(i)了,那这不就是杜教筛水题了嘛。
代码
/*
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 | 1
using 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], 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] + phi[i]) % mod;
}
inv2 = quick_pow(2, mod - 2, mod);
}
ll calc(ll x) {
x %= mod;
return x * (x + 1) % mod * inv2 % mod;
}
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 = calc(x);
for(ll l = 2, r; l <= x; l = r + 1) {
r = x / (x / l);
ans = (ans - (r - l + 1) % mod * get_phi(x / l) % mod + mod) % mod;
}
return ans_phi[x] = ans;
}
ll calc2(ll x) {
x %= mod;
return x * x % mod;
}
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(), ans = 0;
init();
for(ll l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ans = (ans + calc2(n / l) * (get_phi(r) - get_phi(l - 1)) % mod + mod) % mod;
}
cout << ans << endl;
return 0;
}