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

HDU6750 Function

题解

先将题意转化,我们得到
∑ i = 1 n ∑ t ∣ i t [ g c d ( t , i t ) = 1 ] \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{t|i}t[gcd(t,\frac{i}{t})=1] i=1ntit[gcd(t,ti)=1]
化简这个式子即可

推导

∑ i = 1 n ∑ t ∣ i t [ g c d ( t , i t ) = 1 ] = ∑ i = 1 n ∑ t ∣ i t ∑ d ∣ t , d t ∣ i μ ( d ) = ∑ d = 1 n μ ( d ) ∑ t = 1 ⌊ n / d ⌋ t d ∑ i = 1 ⌊ n / d 2 t ⌋ 1 T = d 2 t → = ∑ T = 1 n ( ∑ i = 1 ⌊ n / T ⌋ 1 ) ∑ d 2 ∣ T μ ( d ) T d = ∑ T = 1 n ⌊ n T ⌋ ∑ d 2 ∣ T μ ( d ) T d = ∑ d = 1 n μ ( d ) d ∑ T = 1 ⌊ n / d 2 ⌋ ⌊ n / d 2 T ⌋ T G ( k ) = ∑ i = 1 k ⌊ k i ⌋ i → = ∑ d = 1 n μ ( d ) d G ( ⌊ n d 2 ⌋ ) \begin{aligned} \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{t|i}t[gcd(t,\frac{i}{t})=1] & = \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{t|i} t \displaystyle\sum_{d|t,dt|i}\mu(d) \\ & = \displaystyle\sum_{d = 1} ^ {n} \mu(d) \displaystyle\sum_{t = 1} ^ {\lfloor n / d \rfloor} td \displaystyle\sum_{i = 1} ^ {\lfloor n / d ^ 2 t \rfloor} 1 \\ T = d ^ 2 t \to & = \displaystyle\sum_{T = 1} ^ {n} \left( \displaystyle\sum_{i = 1} ^ {\lfloor n / T \rfloor} 1 \right) \displaystyle\sum_{d ^ 2 | T} \mu(d) \frac{T}{d} \\ & = \displaystyle\sum_{T = 1} ^ {n} \lfloor \frac{n}{T} \rfloor \displaystyle\sum_{d ^ 2 | T} \mu(d) \frac{T}{d} \\ & = \displaystyle\sum_{d = 1} ^ {\sqrt n} \mu(d) d \displaystyle\sum_{T = 1} ^ {\lfloor n / d ^ 2 \rfloor} \lfloor \frac{n / d ^ 2}{T} \rfloor T \\ G(k) = \displaystyle\sum_{i = 1} ^ {k} \lfloor \frac{k}{i} \rfloor i \to & = \displaystyle\sum_{d = 1} ^ {\sqrt n} \mu(d) d G(\lfloor \frac{n}{d ^ 2} \rfloor) \end{aligned} i=1ntit[gcd(t,ti)=1]T=d2tG(k)=i=1kiki=i=1ntitdt,dtiμ(d)=d=1nμ(d)t=1n/dtdi=1n/d2t1=T=1ni=1n/T1d2Tμ(d)dT=T=1nTnd2Tμ(d)dT=d=1n μ(d)dT=1n/d2Tn/d2T=d=1n μ(d)dG(d2n)
这里G函数和原式子都可以用数论分块来做,总体复杂度 O ( n l o g ( n ) ) O(\sqrt n log(n)) O(n log(n))

代码

#include 
using namespace std;
typedef long long ll;
const int MAX = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll inv2 = 500000004;

int vis[MAX], prime[MAX], num, mu[MAX], f[MAX];
ll g[MAX];
void makeMobius(int siz) {
    mu[1] = 1, num = 0;
    for (int i = 2; i <= siz; i++) {
        if (!vis[i]) prime[++num] = i, mu[i] = -1;
        for (int j = 1; j <= num && i * prime[j] <= siz; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0) {
                mu[i * prime[j]] = 0;
                break;
            }
            else mu[i * prime[j]] = mu[i] * mu[prime[j]];
        }
    }
    for (int d = 1; d <= siz; d++)
        for (int i = d; i <= siz; i += d)
            g[i] = (g[i] + d) % mod;
    for (int i = 1; i <= siz; i++) {
        f[i] = (f[i - 1] + 1ll * mu[i] * i % mod) % mod;//求mu * i的前缀和
        g[i] = (g[i] + g[i - 1]) % mod;
    }
}

ll sum(ll x) {
    x %= mod;
    return x * (x + 1) % mod * inv2 % mod;
}

ll calc(ll n) {
    if (n <= 1e6) return g[n];//没有提前筛这东西, T了一发
    ll res = 0;
    for (ll l = 1, r; l <= n; l = r + 1) {
        r = n / (n / l);
        res = (res + 1ll * (sum(r) - sum(l - 1) + mod) % mod * (n / l) % mod) % mod;
    }
    return res;
}

int main() {

    makeMobius(1e6);
    int T; scanf("%d", &T);
    while (T--) {
        ll n; scanf("%lld", &n);
        ll ans = 0;
        for (ll l = 1, r; l * l <= n; l = r + 1) {
            r = l;
            while (n / ((r + 1) * (r + 1)) == n / (r * r)) r++;//这里分块不太会,就这样写了
            ans = (ans + 1ll * calc(n / (l * l)) * ((f[r] - f[l - 1] + mod) % mod) % mod) % mod;
        }
        printf("%lld\n", (ans + mod) % mod);
    }

    return 0;
}

你可能感兴趣的:(数论,#,莫比乌斯反演,数学)