bzoj 4805 欧拉函数求和

http://www.elijahqi.win/archives/3309
Description
给出一个数字N,求sigma(phi(i)),1<=i<=N

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

Output
输出答案。

Sample Input
10
Sample Output
32
HINT

Source

By FancyCoder
公式
i=1ni=i=1nd|n1(d)×φ(nd) ∑ i = 1 n i = ∑ i = 1 n ∑ d | n 1 ( d ) × φ ( n d )
n(n1)2=d=1ni=1ndφ(i) n ∗ ( n − 1 ) 2 = ∑ d = 1 n ∑ i = 1 n d φ ( i )
然后就是很套路的了 把后半段设成要求的 把n=1提取出来算即可

#include<cstdio>
#include<cctype>
#include<algorithm>
#define ll long long
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=2e6+20;
ll phi[N],s[N];int n,prime[N],tot;bool not_prime[N];
inline ll calc(int x){
    if (x<=2e6) return phi[x];if (s[n/x]) return s[n/x];
    ll tmp=(ll)x*x+x>>1;int last;
    for (int i=2;i<=x;i=last+1){
        last=x/(x/i);
        tmp-=(last-i+1)*calc(x/i);
    }return s[n/x]=tmp;
}
int main(){
    freopen("bzoj4805.in","r",stdin);
    for (int i=2;i<=2e6;++i){
        if (!not_prime[i]) prime[++tot]=i,phi[i]=i-1;
        for (int j=1;prime[j]*i<=2e6;++j){
            not_prime[prime[j]*i]=1;
            if (i%prime[j]==0){
                phi[prime[j]*i]=phi[i]*prime[j];break;
            }else phi[prime[j]*i]=phi[i]*phi[prime[j]];
        }
    }phi[1]=1;
    for (int i=2;i<=2e6;++i) phi[i]+=phi[i-1];
    n=read();
    printf("%lld\n",calc(n));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值