Bzoj4804: 欧拉心算

链接
推一下就是 nk=1nk2d|kϕ(d)μ(kd)
d|kϕ(d)μ(kd) 线性筛一下就好

# include 
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e7 + 1);

IL ll Read(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int prime[_], num;
ll f[_];
bool isprime[_];

IL void Prepare(){
    isprime[1] = 1; f[1] = 1;
    for(RG int i = 2; i < _; ++i){
        if(!isprime[i]){  prime[++num] = i; f[i] = i - 2;  }
        for(RG int j = 1; j <= num && i * prime[j] < _; ++j){
            isprime[i * prime[j]] = 1;
            if(i % prime[j])  f[i * prime[j]] = f[i] * f[prime[j]];
            else{
                if((i / prime[j]) % prime[j]) f[i * prime[j]] = f[i / prime[j]] * (prime[j] - 1) * (prime[j] - 1);
                else f[i * prime[j]] = f[i] * prime[j];
                break;
            }
        }
    }
    for(RG int i = 2; i < _; ++i) f[i] += f[i - 1];
}

int main(RG int argc, RG char* argv[]){
    Prepare();
    for(RG ll T = Read(), n, ans; T; --T){
        n = Read(); ans = 0;
        for(RG ll i = 1, j; i <= n; i = j + 1){
            j = n / (n / i);
            ans += (n / i) * (n / i) * (f[j] - f[i - 1]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

你可能感兴趣的:(反演与容斥)