码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj4805: 欧拉函数求和

时间:2018-02-22 23:10:54      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:turn   line   get   void   type   ret   post   pow   2.0   

好久没写杜教筛了
练练手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: 欧拉函数求和

标签:turn   line   get   void   type   ret   post   pow   2.0   

原文地址:https://www.cnblogs.com/cjoieryl/p/8460377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!