Codeforces Round #754 (Div. 2)E 待写

待写

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int n;
int a[maxn], b[maxn];
ll qz[maxn], qzh[maxn], tot;
ll c[maxn], mob[maxn];
int pri[maxn], cnt;
bool nop[maxn];
void init() {
    nop[1] = 1;
    mob[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (!nop[i]) {
            pri[++cnt] = i;
            mob[i] = -1;
        }
        for (int j = 1; j <= cnt && 1ll * i * pri[j] <= n; j++) {
            nop[i * pri[j]] = 1;
            if (i % pri[j] == 0)
                break;
            mob[i * pri[j]] = -mob[i];
        }
    }
}
ll sum(int l, int r) {
    if (l > r)
        return 0;
    return qzh[r] - qzh[l - 1];
}
int main() {
    scanf("%d", &n);
    init();
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &b[i]);
    }
    for (int i = 1; i <= n; i++)
        c[i] = b[i] - a[i];
    c[1] = 0;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i + i; j <= n; j += i) {
            c[j] -= c[i];
        }
    }
    for (int i = 1; i <= n; i++) {
        if (mob[i] == 0)
            ans += llabs(c[i]);
        else if (mob[i] == -1)
            qz[++tot] = -c[i];
        else
            qz[++tot] = c[i];
    }
    sort(qz + 1, qz + 1 + tot);
    for (int i = 1; i <= tot + 1; i++)
        qzh[i] = qzh[i - 1] + qz[i];
    int q;
    scanf("%d", &q);
    while (q--) {
        int x;
        scanf("%d", &x);
        x -= a[1];
        int wz = lower_bound(qz + 1, qz + 1 + tot, -x) - qz;
        ll nans = sum(wz, tot) + 1ll * (tot - wz + 1) * x;
        nans -= sum(1, wz - 1) + 1ll * (wz - 1) * x;
        printf("%lld\n", ans + nans);
    }
    return 0;
}
上一篇:预处理+条件的维度减少


下一篇:lgP4727 [HNOI2009]图的同构计数