zoukankan      html  css  js  c++  java
  • 【BZOJ4805】欧拉函数求和

    题面

    Description

    给出一个数字N,求(sumlimits_{i=1}^nvarphi(i))i,1<=i<=N

    Input

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

    Output

    输出答案。

    Sample Input

    10

    Sample Output

    32

    题目分析

    杜教筛模板题。

    ((1*varphi)=Id),取(g(x)=1)

    [S(n)=frac {n cdot (n+1)}2-sum_{i=2}^nS(frac ni) ]

    代码实现

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<map>
    #define MAXN 0x7fffffff
    typedef long long LL;
    const int N=1e7+5,M=1e7;
    using namespace std;
    inline int Getint(){register int x=0,f=1;register char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}return x*f;}
    bool vis[N];
    int prime[N];
    LL phi[N];
    map<LL,LL>sphi;
    LL Sphi(int x){
    	if(x<=M)return phi[x];
    	if(sphi[x])return sphi[x];
    	LL ret=1ll*x*(x+1)/2;
    	for(int l=2,r;l<=x;l=r+1){
    		r=x/(x/l);
    		ret-=(r-l+1)*Sphi(x/l);
    	}
    	return sphi[x]=ret;
    } 
    int main(){
    	phi[1]=1;
    	for(int i=2;i<=M;i++){
    		if(!vis[i])prime[++prime[0]]=i,phi[i]=i-1;
    		for(int j=1;j<=prime[0]&&1ll*prime[j]*i<=M;j++){
    			vis[i*prime[j]]=1;
    			if(i%prime[j]==0){
    				phi[i*prime[j]]=phi[i]*prime[j];
    				break;
    			}
    			phi[i*prime[j]]=phi[i]*phi[prime[j]];
    		}
    	}
    	for(int i=2;i<=M;i++)phi[i]+=phi[i-1];
    	int n=Getint(); 
    	cout<<Sphi(n); 
    	return 0;
    }
    
  • 相关阅读:
    文件打开的几种访问模式
    数据分析师简介
    python数据处理----常用数据文件的处理
    markdown使用方法介绍
    gdb调试常用方法介绍
    [OPEN CV] 常用视频操作方法
    [转载]C++中四种强制类型转换方式
    python 定时服务模块
    pymysql安装和使用
    VS2019开发Qt程序中文乱码
  • 原文地址:https://www.cnblogs.com/Emiya-wjk/p/10011394.html
Copyright © 2011-2022 走看看