您的位置:首页 > 其它

bzoj 4805: 欧拉函数求和 杜教筛

2017-04-12 11:02 232 查看

题意

给出一个数字N,求sigma(phi(i)),1<=i<=N。

正整数N。N<=2*10^9

分析

bzoj 3994

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long LL;

const int N=5000005;
const int MOD=1000000007;

int tot,prime[N/10];
bool not_prime
;
LL phi
;

void get_prime(int n)
{
phi[1]=1;
for (int i=2;i<=n;i++)
{
if (!not_prime[i]) prime[++tot]=i,phi[i]=i-1;
for (int j=1;j<=tot&&i*prime[j]<=n;j++)
{
not_prime[i*prime[j]]=1;
if (i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for (int i=1;i<=n;i++) phi[i]+=phi[i-1];
}

LL solve(int n)
{
if (n<=5000000) return phi
;
LL ans=(LL)n*(n+1)/2;
for (int i=2,last;i<=n;i=last+1)
{
last=n/(n/i);
ans-=(LL)(last-i+1)*solve(n/i);
}
return ans;
}

int main()
{
get_prime(5000000);
int n;
scanf("%d",&n);
printf("%lld",solve(n));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: