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

来源:互联网 发布:android登录注册源码 编辑:程序博客网 时间:2024/06/07 10:23

题意

给出一个数字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[N];LL phi[N];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[n];    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;}
0 1
原创粉丝点击