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)\) 数据范围线性不可做. ...
随机推荐
- LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- OpenCV-Python在图片上输出中文
OpenCV中在图片上输出中文一般需要借助FreeType库实现.FreeType库是一个完全免费(开源)的.高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件.但使用FreeType ...
- UGUI技巧
http://www.cnblogs.com/suoluo/p/5427514.html Text中的可以单独指定某些文字的颜色,只需将想要变色的文本放在<color=**></co ...
- NAT打洞
NAT(Network Address Translation)是一种广域网的接入技术,将私有地址转换为合法的公共IP地址,可以完美的解决IP地址不足问题,而且还能有效避免来自外部网络的攻击,隐藏并保 ...
- angular的$watch,$digest和$apply
第一部分:$watch $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEqu ...
- dockerfile http_php
FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...
- java List 等份截取
/** * 描述: 等份截取 * @author eyesmooon * @param list * @param size * @return * @date:2018年11月8日 下午8:00:3 ...
- maven 历史版本下载
1.登录http://maven.apache.org/download.cgi 2.拉倒最下面,点击 archives 3.可以看到maven个版本,找自己需要的下载
- GXT4.0 BorderLayoutContainer布局
T字型布局. @Override public void onModuleLoad() { BorderLayoutContainer con = new BorderLayoutContainer( ...
- HDU1576(扩展欧几里得)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...