2019年南昌邀请赛G题

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int mod = (1<<30);
const int maxn = 1e7 +7;

int t, n, cnt;
bool v[maxn];
int isp[maxn], mu[maxn], phi[maxn];
LL sum[maxn];

void init() {
    mu[1] = 1;
    for(int i = 2; i < maxn; ++i) {
        if(!v[i]) {
            v[i] = 1;
            isp[cnt++] = i;
            mu[i] = -1;
            phi[i] = i - 1;
            sum[i] = (phi[i] * mu[i] + mod) % mod;
        }
        for(int j = 0; j < cnt; ++j) {
            if(i * isp[j] > maxn) break;
            v[i*isp[j]] = 1;
            if(i % isp[j] == 0) {
                mu[i*isp[j]] = 0;
                phi[i*isp[j]] = phi[i] * isp[j];
                (sum[i*isp[j]] += phi[i] * mu[j] + mod) %= mod;
                break;
            }
            mu[i*isp[j]] = -mu[i];
            phi[i*isp[j]] = phi[i] * (isp[j] - 1);
            (sum[i*isp[j]] += phi[i] * mu[j] + mod) %= mod;
        }
    }
}

int qpow(int x, int n) {
    int res = 1;
    while(n) {
        if(n & 1) res = 1LL * res * x % mod;
        x = 1LL * x * x % mod;
        n >>= 1;
    }
    return res;
}

int main() {
    scanf("%d", &t);
    int inv = qpow(3, mod - 2);
    init();
    while(t--) {
        scanf("%d", &n);
        LL ans = (((1LL * n * n % mod * n % mod + 1LL * (n + 1) * (n + 1) % mod) % mod) * (2 * n + 1) % mod) * inv % mod;
        for(int l = 1, r; l <= n; l = r + 1) {
            r = min(n, n / (n / l));
            LL x = 1LL * (n / l) * (n / l);
            ans = (ans + sum[l] * x % mod * (r - l + 1) % mod) % mod;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
上一篇:硬件笔记(10)----单片机ISP(在线编程)实现


下一篇:1.2 Interview 1.2.3 Background Reading(II)