杜教筛
这是一种对于一个数论函数
构造两个积性函数
然后我们得到
记忆化之后复杂度是
如果预处理
当
欧拉函数怎么独角骰?欧拉函数怎么独角骰?欧拉函数怎么独角骰?
首先有一个
梅比乌斯怎么独角晒?梅比乌斯怎么独角晒?梅比乌斯怎么独角晒?
首先有一个
#include<bits/stdc++.h>
#define int long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
using namespace std;
const int N=5000010;
int t, n, k=5e6;
int vis[N], p[N], top, phi[N], mu[N];
map<int,int> sphi, smu;
void init() {
vis[0]=vis[1]=phi[1]=mu[1]=1;
up(i,2,k) {
if(!vis[i]) p[++top]=i, phi[i]=i-1, mu[i]=-1;
for(int j=1; j<=top&&i*p[j]<=k; ++j) {
int x=i*p[j]; vis[x]=1;
if(i%p[j]) mu[x]=-mu[i], phi[x]=phi[i]*(p[j]-1);
else { mu[x]=0, phi[x]=phi[i]*p[j]; break; }
}
}
up(i,1,k) phi[i]+=phi[i-1], mu[i]+=mu[i-1];
}
int getphi(int x) {
if(x<=k) return phi[x];
if(sphi.find(x)!=sphi.end()) return sphi[x];
int res=(1+x)*x/2;
for(int l=2, r; l<=x; l=r+1) {
r=min(x,x/(x/l));
res-=(r-l+1)*getphi(x/l);
}
return sphi[x]=res;
}
int getmu(int x) {
if(x<=k) return mu[x];
if(smu.find(x)!=smu.end()) return smu[x];
int res=1;
for(int l=2, r; l<=x; l=r+1) {
r=min(x,x/(x/l));
res-=(r-l+1)*getmu(x/l);
}
return smu[x]=res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
init(), cin >> t;
while(t--) {
cin >> n;
cout << getphi(n) << ' ' << getmu(n) << '\n';
}
return 0;
}