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;
}