[题目链接] https://www.luogu.org/problemnew/show/P4213

给定一个正整数\(N(N\le2^{31}-1)\)

\(ans_1=\sum_{i=1}^n\varphi(i)\)

\(ans_2=\sum_{i=1}^n \mu(i)\)

[题解] https://acfcacfca.blog.luogu.org/dls-tql

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<tr1/unordered_map>
//#define int long long
using namespace std;
typedef long long LL;
const int INF=1e9+7;
inline LL read(){
register LL x=0,f=1;register char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
return f*x;
} const int MAXN=5e6+5; LL mu[MAXN];LL phi[MAXN];
int prime[MAXN];bool vis[MAXN];
unordered_map <int,LL> Smu,Sphi;
int n,T; inline void init(int n){
mu[1]=1,phi[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){
prime[++prime[0]]=i;
mu[i]=-1;
phi[i]=i-1;
}
int x;
for(int j=1;j<=prime[0]&&(x=(i*prime[j]))<=n;j++){
vis[x]=1;
if(i%prime[j]==0){
mu[x]=0;
phi[x]=1ll*phi[i]*prime[j];
break;
}
mu[x]=-mu[i];
phi[x]=1ll*phi[i]*phi[prime[j]];
}
}
for(int i=2;i<=n;i++)
mu[i]+=mu[i-1],phi[i]+=phi[i-1];
} inline LL S_mu(int x){
if(x<MAXN) return mu[x];
if(Smu[x]) return Smu[x];
LL res=1;
for(int l=2,r;l<=x;l=r+1){
r=x/(x/l);
res-=1ll*(r-l+1)*S_mu(x/l);
}
return Smu[x]=res;
} inline LL S_phi(int x){
if(x<MAXN) return phi[x];
if(Sphi[x]) return Sphi[x];
LL res=1LL*x*(x+1)/2;//对int敏感!!
for(int l=2,r;l<=x;l=r+1){
r=x/(x/l);
res-=1LL*(r-l+1)*S_phi(x/l);
}
return Sphi[x]=res;
} signed main(){
T=read();
init(MAXN-1);
while(T--){
n=read();
printf("%lld %lld\n",S_phi(n),S_mu(n));
}
}

最新文章

  1. struts2验证框架
  2. Asp.net 后台添加Meta标签方法
  3. 史上最全github使用方法:github入门到精通--备用
  4. 2013腾讯编程马拉松初赛第一场(3月21日) 湫湫系列故事——减肥记II ----线段树
  5. winfrom 倒计时控件
  6. GridView中两个DropDownList联动
  7. Delphi+GDI
  8. java架构师负载均衡、高并发、nginx优化、tomcat集群、异步性能优化、Dubbo分布式、Redis持久化、ActiveMQ中间件、Netty互联网、spring大型分布式项目实战视频教程百度网盘
  9. [转]在Mac系统中安装配置Tomcat及和Eclipse 配置
  10. Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
  11. 转载 线程池之ThreadPool类与辅助线程 - &lt;第二篇&gt;
  12. Python paramiko ssh 在同一个session里run多个命令
  13. 9:@RequestMapping 用法详解之地址映射
  14. asp.net mvc用aspose.cells 导出xlsx格式的excel。无残留
  15. [转]VS中展开和折叠代码
  16. Python学习之路day3-集合
  17. jQuery解决IE6、7、8不能使用 JSON.stringify 函数的问题
  18. SQL Server 数据库存储过程实例
  19. C#委托和事件定义和使用
  20. iptables-linux(ls)-inode-block

热门文章

  1. 记一次完整的android源码截屏事件的捕获&lt;标记砖&gt;
  2. SSM项目连接远程Linux服务器的mysql 启动tomcat卡在了 Initializing Spring root WebApplicationContext
  3. php中用大括号把?&gt;和&lt;?php框起来的作用
  4. 在PyCharm 软件中设置你的项目 使用的Python版本
  5. 关于photoshop处理图片的自动化
  6. 说说excel
  7. 内核文件ntoskrnl.exe,ntkrnlpa.exe的区别??
  8. 对Spark的理解
  9. 解决.jsp及静态资源文件访问404的问题
  10. 按失真类型分类整理IQA数据集:TID2013