p4213 【模板】杜教筛(Sum)
2024-09-06 23:59:45
分析
我们知道
$\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 ;
}
最新文章
- nginx 虚拟主机配置
- 谷毅(WingKu)横空出世
- Android annotations REST
- Android HandlerThread 的使用及其Demo
- vs2010工具栏 不显示dx控件
- python概要
- UNIX网络编程——网络数据包检测
- 就这样CSDN账号被人盗了??
- 使用VSCode 编译调试QT程序
- Android 使用EventBus发送消息接收消息
- Problem C: Pie
- 【Unity Shader实战】卡通风格的Shader(一)
- redis持久化详述
- Mybatis关联查询之一对多和多对一XML配置详解
- python第三方包安装方法(两种方法)
- [微信开发] 微信JSAPI - 获取用户地理位置信息
- 【react】---手动封装一个简易版的redux---【巷子】
- CSS 分类 选择器
- 洛谷P4549裴蜀定理
- E/MediaPlayer: start called in state 4, mPlayer(0xcc719a40)解决