HDU 6750 Function(莫比乌斯反演)(2020百度之星初赛1)

tech2024-12-26  13

Function

推式子

S ( n ) = ∑ i = 1 n ∑ d ∣ i d [ g c d ( d , i d ) = = 1 ] = ∑ d = 1 n d ∑ d ∣ i [ g c d ( d , i d ) = = 1 ] = ∑ d = 1 n d ∑ i = 1 n d [ g c d ( d , i ) = = 1 ] = ∑ d = 1 n d ∑ i = 1 n d ∑ k ∣ g c d ( d , i ) μ ( k ) = ∑ k = 1 n μ ( k ) k ∑ d = 1 n k d ∑ i = 1 n k 2 d t = k 2 d = ∑ t = 1 n n t ∑ k 2 ∣ t μ ( k ) k t k 2 = ∑ k = 1 n μ ( k ) k ∑ k 2 ∣ t n t t k 2 i = t k 2 = ∑ k = 1 n μ ( k ) k ∑ i = 1 n k 2 n i k 2 i S(n) = \sum_{i = 1} ^{n} \sum_{d \mid i} d [gcd(d, \frac{i}{d}) == 1]\\ = \sum_{d = 1} ^{n} d \sum_{d \mid i} [gcd(d, \frac{i}{d}) == 1]\\ = \sum_{d = 1} ^{n}d \sum_{i = 1} ^{\frac{n}{d}}[gcd(d, i)== 1]\\ = \sum_{d = 1} ^{n}d \sum_{i = 1} ^{\frac{n}{d}} \sum_{k \mid gcd(d, i)} \mu(k)\\ = \sum_{k = 1} ^{n} \mu(k) k \sum_{d = 1} ^{\frac{n}{k}} d \sum_{i = 1} ^{\frac{n}{k ^2d}}\\ t = k ^ 2 d\\ = \sum_{t = 1} ^{n} \frac{n}{t} \sum_{k ^ 2 \mid t} \mu(k)k \frac{t}{k ^ 2}\\ = \sum_{k = 1} ^{\sqrt n} \mu(k)k \sum_{k ^ 2 \mid t} \frac{n}{t} \frac{t}{k ^ 2}\\ i = \frac{t}{k ^ 2}\\ = \sum_{k = 1} ^{\sqrt n} \mu(k) k \sum_{i = 1} ^{\frac{n}{k ^ 2}} \frac{n}{i k ^ 2} i\\ S(n)=i=1ndid[gcd(d,di)==1]=d=1nddi[gcd(d,di)==1]=d=1ndi=1dn[gcd(d,i)==1]=d=1ndi=1dnkgcd(d,i)μ(k)=k=1nμ(k)kd=1kndi=1k2dnt=k2d=t=1ntnk2tμ(k)kk2t=k=1n μ(k)kk2ttnk2ti=k2t=k=1n μ(k)ki=1k2nik2ni

代码

注意随手取模,long long × \times ×long long会溢出!!!

/* Author : lifehappy */ #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const double eps = 1e-7; const int N = 1e6 + 10, mod = 1e9 + 7, inv2 = mod + 1 >> 1; int prime[N], cnt; ll mu[N]; bool st[N]; void init() { mu[1] = 1; for(int i = 2; i < N; i++) { if(!st[i]) { prime[cnt++] = i; mu[i] = -1; } for(int j = 0; j < cnt && i * prime[j] < N; j++) { st[i * prime[j]] = 1; if(i % prime[j] == 0) break; mu[i * prime[j]] = -mu[i]; } } for(int i = 1; i < N; i++) { mu[i] = (mu[i - 1] + 1ll * i * mu[i] % mod + mod) % mod; } } ll calc1(ll l, ll r) { return 1ll * (l + r) % mod * ((r - l + 1) % mod) % mod * inv2 % mod; } ll calc2(ll n) { ll ans = 0; for(ll l = 1, r; l <= n; l = r + 1) { r = n / (n / l); ans = (ans + 1ll * (n / l) % mod * calc1(l, r) % mod) % mod; } return ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); int T; scanf("%d", &T); while(T--) { ll n, ans = 0; scanf("%lld", &n); int m = sqrt(n); for(ll l = 1, r; l <= m; l = r + 1) { r = min((int)sqrt(n / (n / (l * l))), m); ans = (ans + 1ll * ((mu[r] - mu[l - 1]) % mod + mod) % mod * calc2(n / (l * l)) % mod) % mod; } printf("%lld\n", ans); } return 0; }
最新回复(0)