Bzoj4805: 欧拉函数求和

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bzoj4805: 欧拉函数求和相关的知识,希望对你有一定的参考价值。

好久没写杜教筛了
练练手AC量刷起

# include <bits/stdc++.h>
# 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 int Input(){
    RG int 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, n, tot;
ll phi[_];
map <ll, ll> Phi;
bool isprime[_];

IL void Sieve(RG int m){
    isprime[1] = 1, phi[1] = 1, tot = m;
    for(RG int i = 2; i <= m; ++i){
        if(!isprime[i]) prime[++num] = i, phi[i] = i - 1;
        for(RG int j = 1; j <= num && i * prime[j] <= m; ++j){
            isprime[i * prime[j]] = 1;
            if(i % prime[j]) phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            else{
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
        }
    }
    for(RG int i = 2; i <= m; ++i) phi[i] += phi[i - 1];
}

IL ll S(RG ll x){
    return x * (x + 1) >> 1;
}

IL ll Du_Sieve(RG ll m){
    if(m <= tot) return phi[m];
    if(Phi[m]) return Phi[m];
    RG ll ret = m * (m + 1) >> 1;
    for(RG int i = 2, j; i <= m; i = j + 1){
        j = m / (m / i);
        ret -= 1LL * (j - i + 1) * Du_Sieve(m / i);
    }
    return Phi[m] = ret;
}

int main(RG int argc, RG char* argv[]){
    n = Input();
    Sieve(pow(n, 2.0 / 3.0));
    printf("%lld\n", Du_Sieve(n));
    return 0;
}

以上是关于Bzoj4805: 欧拉函数求和的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ4805欧拉函数求和(杜教筛)

Bzoj4805: 欧拉函数求和

BZOJ 4805 欧拉函数求和

[BZOJ]4805: 欧拉函数求和

BZOJ3944/4805Sum/欧拉函数求和 杜教筛

BZOJ 2190 仪仗队(线性筛欧拉函数)