Battlestation Operational HDU 6134

Battlestation Operational

∑ i = 1 n ∑ j = 1 i ⌈ i j ⌉ [ g c d ( i , j ) = 1 ] ∑ i = 1 n ∑ j = 1 i ( i j + 1 ) [ g c d ( i , j ) = 1 ] − ∑ i = 1 n ∑ j = 1 i [ g c d ( i , j ) = 1 , j ∣ i ] ∑ i = 1 n ∑ j = 1 i ( i j + 1 ) [ g c d ( i , j ) = 1 ] − n ∑ i = 1 n ∑ j = 1 i i j [ g c d ( i , j ) = 1 ] + ∑ i = 1 n ∑ j = 1 i [ g c d ( i , j ) = 1 ] − n ∑ i = 1 n ∑ j = 1 i i j [ g c d ( i , j ) = 1 ] + ∑ i = 1 ϕ ( i ) − n ∑ i = 1 n ∑ j = 1 i i j [ g c d ( i , j ) = 1 ] ∑ d = 1 n μ ( d ) ∑ i = 1 n d ∑ j = 1 i i j 求 解 ∑ i = 1 n ∑ j = 1 i i j = ∑ i = 1 n ∑ j = i n j i 可 以 发 现 这 个 东 西 具 有 区 间 分 块 性 质 所 以 可 以 利 用 差 分 然 后 求 前 缀 和 得 到 , 具 体 实 现 看 代 码 整 体 复 杂 度 O ( n log ⁡ n + T × n ) \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \lceil \frac{i}{j} \rceil [gcd(i, j) = 1]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i}( \frac{i}{j} + 1)[gcd(i, j) = 1] - \sum_{i = 1} ^{n}\sum_{j = 1} ^{i}[gcd(i, j) = 1, j \mid i]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i}(\frac{i}{j} + 1)[gcd(i, j) = 1] - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j) = 1] + \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} [gcd(i, j) = 1] - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j) = 1] + \sum_{i = 1} \phi(i) - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j)= 1]\\ \sum_{d = 1} ^{n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{i} \frac{i}{j}\\ 求解\sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j} = \sum_{i = 1} ^{n} \sum_{j = i} ^{n} \frac{j}{i}\\ 可以发现这个东西具有区间分块性质\\所以可以利用差分然后求前缀和得到,具体实现看代码\\ 整体复杂度O(n \log n + T \times \sqrt n)\\ i=1∑n​j=1∑i​⌈ji​⌉[gcd(i,j)=1]i=1∑n​j=1∑i​(ji​+1)[gcd(i,j)=1]−i=1∑n​j=1∑i​[gcd(i,j)=1,j∣i]i=1∑n​j=1∑i​(ji​+1)[gcd(i,j)=1]−ni=1∑n​j=1∑i​ji​[gcd(i,j)=1]+i=1∑n​j=1∑i​[gcd(i,j)=1]−ni=1∑n​j=1∑i​ji​[gcd(i,j)=1]+i=1∑​ϕ(i)−ni=1∑n​j=1∑i​ji​[gcd(i,j)=1]d=1∑n​μ(d)i=1∑dn​​j=1∑i​ji​求解i=1∑n​j=1∑i​ji​=i=1∑n​j=i∑n​ij​可以发现这个东西具有区间分块性质所以可以利用差分然后求前缀和得到,具体实现看代码整体复杂度O(nlogn+T×n ​)

/*
  Author : lifehappy
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e6 + 10, mod = 1e9 + 7;

int prime[N], phi[N], mu[N], f[N], cnt, n;

bool st[N];

void init() {
    mu[1] = phi[1] = 1;
    for(int i = 2; i < N; i++) {
        if(!st[i]) {
            prime[++cnt] = i;
            mu[i] = -1;
            phi[i] = i - 1;
        }
        for(int j = 1; 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);
            mu[i * prime[j]] = -mu[i];
        }
    }
    for(int i = 1; i < N; i++) {
        for(int j = i; j < N; j += i) {
            int l = j, r = j + i;
            f[l] = (f[l] + (j / i)) % mod;
            if(r < N) f[r] = (f[r] - (j / i) + mod) % mod;
        }
    }
    for(int i = 1; i < N; i++) {
        phi[i] = (phi[i] + phi[i - 1]) % mod;
        mu[i] = (mu[i] + mu[i - 1] + mod) % mod;
        f[i] = (f[i] + f[i - 1]) % mod;
    }
    for(int i = 1; i < N; i++) {
        f[i] = (f[i] + f[i - 1]) % mod;
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    init();
    while(scanf("%d", &n) != EOF) {
        ll ans = 0;
        for(int l = 1, r; l <= n; l = r + 1) {
            r = n / (n / l);
            ans = (ans + 1ll * (mu[r] - mu[l - 1]) * f[n / l] % mod + mod) % mod;
        }
        ans = (ans + phi[n] - n + mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}
上一篇:PCL 1.12.0 在 win10 vs2019下的安装与源代码编译。


下一篇:辅助角公式中系数含参