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)\) 数据范围线性不可做. ...
随机推荐
- hdoj-1031-Design T-Shirt
题目链接 /* 两次排序,搞定 */ #include <cstring> #include <iostream> #include <algorithm> usi ...
- KVO与KVC的使用(转)
转自http://www.cnblogs.com/mybkn/archive/2013/05/29/3104166.html KVO: 在看KVO之前,有必要先了解下KVC,即,Key-Value C ...
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- jsp有哪些动作?作用分别是什么?
jsp共有6种基本动作: 1.jsp:include,在页面被请求的时候引入一个文件 2.jsp:useBean,寻找或者实例化一个JavaBean 3.jsp:setProperty,设置JavaB ...
- 使用navicat进行数据表迁移
使用navicat进行数据和表迁移只需要复制,粘贴就可以实现.
- walle部署系统的使用
在项目开发的时候要管理各种开发 测试 线上环境的代码 部署 回滚等操作 这里可以使用walle walle官网:http://www.walle-web.io/ 学习安装:https://blog.c ...
- laravel csrf保护
有时候我们的项目需要和外部的项目进行接口对接,如果是post的方式请求;laravel要求csrf保护 但是别人是ci框架或者没有csrf_token的;该如何处理呢? 可以把我们不需要csrf的ur ...
- Android 4学习(2):概述 - AndroidManifes.xml
Android应用程序包含下面这几个部分: 程序的前后台: Activities Services 存储: Content Providers 消息传递: Intents Broadcast Rece ...
- java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和总结
在web开发中,避免不了对日期的操作,就几种常见的日期操作做个总结(部分参考网络,在此表示感谢): java.util.Date.java.sql.Date.java.sql.Time.java.sq ...