题面

Description

给出一个数字N,求\(\sum\limits_{i=1}^n\varphi(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;
}

【BZOJ4805】欧拉函数求和的更多相关文章

  1. BZOJ4805: 欧拉函数求和(杜教筛)

    4805: 欧拉函数求和 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 614  Solved: 342[Submit][Status][Discus ...

  2. Bzoj4805: 欧拉函数求和

    好久没写杜教筛了 练练手AC量刷起 # include <bits/stdc++.h> # define RG register # define IL inline # define F ...

  3. 【BZOJ4805】欧拉函数求和(杜教筛)

    [BZOJ4805]欧拉函数求和(杜教筛) 题面 BZOJ 题解 好久没写过了 正好看见了顺手切一下 令\[S(n)=\sum_{i=1}^n\varphi(i)\] 设存在的某个积性函数\(g(x) ...

  4. poj3090欧拉函数求和

    E - (例题)欧拉函数求和 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     ...

  5. 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛

    bzoj3944 题目描述 输入 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 输出 一共T行,每行两个用空格分隔的数ans1,ans2 样例输 ...

  6. HDU2824-The Euler function-筛选法求欧拉函数+求和

    欧拉函数: φ(n)=n*(1-1/p1)(1-1/p2)....(1-1/pk),其中p1.p2-pk为n的所有素因子.比如:φ(12)=12*(1-1/2)(1-1/3)=4.可以用类似求素数的筛 ...

  7. [BZOJ]4805: 欧拉函数求和

    解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i)  ...

  8. BZOJ 4805: 欧拉函数求和 杜教筛

    https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...

  9. 【BZOJ3944/4805】Sum/欧拉函数求和 杜教筛

    [BZOJ3944]Sum Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用 ...

随机推荐

  1. IIS反向代理解决Web前端跨域

    1.1 IIS7反向代理解决跨域问题IIS的版本必须是IIS7及其以上,否则没有反向代理功能:按照以下步骤来配置IIS,以实现反向代理: 1.2 配置步骤1. 下载安装ARR(Application ...

  2. sqlalchemy session

    Cookie cookie是浏览器保存在用户电脑上的一小段文本,用来保存用户在网站上的必要的信息.Web页面或服务器告诉浏览器按照一定的规范存储这些信息,并且在以后的所有请求中,这些信息就会自动加在h ...

  3. Python-爬虫之股转系统下载文件自动翻页

    上次代码只能抓取一个网页上的链接,本次可以自主设定抓取的页面个数. 代码如下: from selenium import webdriver import os, time class Downloa ...

  4. 3、发送第一个api请求

    接口地址:https://www.v2ex.com/api/topics/latest.json Method: GET Authentication: None 我们打开postman,方法选择ge ...

  5. 获取从天气预报接口返回回来的json数据

    搬迁到了我的新博客中 ==> http://www.suanliutudousi.com/2017/08/26/%E8%8E%B7%E5%8F%96%E4%BB%8E%E5%A4%A9%E6%B ...

  6. spring boot junit test

    这里分三种,1.测普通方法或通过原生java API接口调用 2.基于spring依赖注入调用 3.controller层调用 需要引入依赖:默认springboot已经引入 <dependen ...

  7. Ajax加载数据的使用

    需求就是不能再进入页面时加载数据,只能在点击其中一个按钮时把数据加载呈现出来.具体效果如最下面的图. 1.前台页面 <h1 " onclick="GetData(1)&quo ...

  8. SQL一些记录

    1,2字段约束create unique index [索引名] on 软件信息表(S_SName,S_Edition)

  9. vue-router 的重定向-redirect

    1. 我们主要在index.js重新设置路由的重新定向redirect参数 index.js import Vue from 'vue' import Router from 'vue-router' ...

  10. Android 开发 框架系列 OkHttp文件下载功能实现(含断点续传)

    前言 此篇博客只是下载功能的记录demo,如果你还不太了解okhttp可以参考我的另一篇博客https://www.cnblogs.com/guanxinjing/p/9708575.html 代码部 ...