p4213 【模板】杜教筛(Sum)
分析
我们知道
$\varphi * 1 = id$
$\mu * 1 = e$
杜教筛即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<unordered_map>
using namespace std;
const int N = 5e6;
unordered_map<int,long long>phi;
unordered_map<int,int>mu;
unordered_map<int,bool>visp,vism;
long long _p[N+];
int _m[N+];
int cnt,p[N];
bool is[N+];
inline long long get_phi(int x){
if(x<=N)return _p[x];
if(visp[x])return phi[x];
long long res=(long long)x*(x+)/;
int le=,ri;
for(;le<=x;le=ri+){
ri=x/(x/le);
res-=(ri-le+)*get_phi(x/le);
}
visp[x]=;return phi[x]=res;
}
inline int get_mu(int x){
if(x<=N)return _m[x];
if(vism[x])return mu[x];
int res=,le=,ri;
for(;le<=x;le=ri+){
ri=x/(x/le);
res-=(ri-le+)*get_mu(x/le);
}
vism[x]=;return mu[x]=res;
}
inline void go(){
register int i,j,k;
_p[]=_m[]=;
for(i=;i<=N;++i){
if(!is[i])p[++cnt]=i,_m[i]=-,_p[i]=i-;
for(j=;j<=cnt,i*p[j]<=N;++j){
is[p[j]*i]=;
if(i%p[j]==){
_m[i*p[j]]=;
_p[i*p[j]]=_p[i]*p[j];
break;
}
_m[i*p[j]]=-_m[i];
_p[i*p[j]]=_p[i]*(p[j]-);
}
}
for(i=;i<=N;++i)_p[i]+=_p[i-],_m[i]+=_m[i-];
}
int main(){
int n,t;
scanf("%d",&t);
go();
while(t--){
scanf("%d",&n);
printf("%lld %d\n",get_phi(n),get_mu(n));
}
return ;
}
p4213 【模板】杜教筛(Sum)的更多相关文章
- [模板] 杜教筛 && bzoj3944-Sum
杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...
- luoguP4213 [模板]杜教筛
https://www.luogu.org/problemnew/show/P4213 同 bzoj3944 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...
- 洛谷P4213(杜教筛)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...
- LG4213 【模板】杜教筛(Sum)和 BZOJ4916 神犇和蒟蒻
P4213 [模板]杜教筛(Sum) 题目描述 给定一个正整数$N(N\le2^{31}-1)$ 求 $$ans_1=\sum_{i=1}^n\varphi(i)$$ $$ans_2=\sum_{i= ...
- 51NOD 1222 最小公倍数计数 [莫比乌斯反演 杜教筛]
1222 最小公倍数计数 题意:求有多少数对\((a,b):a<b\)满足\(lcm(a,b) \in [1, n]\) \(n \le 10^{11}\) 卡内存! 枚举\(gcd, \fra ...
- [洛谷P4213]【模板】杜教筛(Sum)
题目大意:给你$n$,求:$$\sum\limits_{i=1}^n\varphi(i),\sum\limits_{i=1}^n\mu(i)$$最多$10$组数据,$n\leqslant2^{31}- ...
- P4213 【模板】杜教筛(Sum)
\(\color{#0066ff}{题 目 描 述}\) 给定一个正整数\(N(N\le2^{31}-1)\) 求 \(\begin{aligned} ans_1=\sum_{i=1}^n\varph ...
- P4213【模板】杜教筛(Sum)
思路:杜教筛 提交:\(2\)次 错因:\(\varphi(i)\)的前缀和用\(int\)存的 题解: 对于一类筛积性函数前缀和的问题,杜教筛可以以低于线性的时间复杂度来解决问题. 先要构造\(h= ...
- BZOJ3944: Sum(杜教筛模板)
BZOJ3944: Sum(杜教筛模板) 题面描述 传送门 题目分析 求\(\sum_{i=1}^{n}\mu(i)\)和\(\sum_{i=1}^{n}\varphi(i)\) 数据范围线性不可做. ...
随机推荐
- poj2010
大学招n(n为奇数)个牛 招第i个牛需要ai块钱 第i个牛高考si分 输入招的牛数n 总的牛数c 总的钱数f 以及ai si 问用这些钱招的n个牛高考分数的中位数最大是多少 如果钱不够输出-1 这题结 ...
- Django 登录页面重定向
转自:http://blog.chedushi.com/archives/3484 登陆和注销操作在网页编程上很常见,这两个操作经常需要在操作成功以后转入发出请求的页面. 比如用户正在浏览一篇文章,发 ...
- C#异步编程(三)内核模式线程同步
其实,在开发过程中,无论是用户模式的同步构造还是内核模式,都应该尽量避免.因为线程同步都会造成阻塞,这就影响了我们的并发量,也影响整个应用的效率.不过有些情况,我们不得不进行线程同步. 内核模式 wi ...
- Jquery中.ajax和.post详解
之前写过一篇<.NET MVC 异步提交和返回参数> ,里面有一些ajax的内容,但是不深入,这次详细剖析下jquery中$.ajax的用法. 首先,上代码: jquery-1.5.1 $ ...
- BZOJ4195:[NOI2015]程序自动分析
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- ping错误详解
在网络中Ping 是一个十分好用的TCP/IP工具,它主要的功能是用来检测网络的连通情况和分析网络速度. 输入 ping /? 例出ping的参数 使用Ping检查连通性有五个步骤 1. 使用ipco ...
- 基于TCP协议 I/O多路转接(select) 的高性能回显服务器客户端模型
服务端代码: myselect.c #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> ...
- ORACLE增加用户
create user 账号 identified by "密码"; grant connect to 账号; grant resource to 账号; --把dba 权限给in ...
- Spring 与 MyBatis 的整合
本文讨论 Spring 与 MyBatis 的整合. 在 beans.xml 中我们定义了两个 bean: SqlSessionFactoryBean.SqlSessionTemplate. 1.Sq ...
- 第九课 go的循环语句
1 for语句的三种形式 /* for 循环 */ ; a < ; a++ { fmt.Printf("a 的值为: %d\n", a) } var a, b int = 1 ...