const int N = 1e8 + 10;
bool vis[N];
ll prime[N],mu[N],phi[N], cnt = 0;//mu莫比乌斯函数,phi欧拉函数
void init(ll n) {
mu[1] = phi[1]=1;
for (int i = 0; i <= n; ++i)vis[i] = false;
for (int i = 2; i <= n; ++i) {
if (!vis[i]) {
prime[++cnt] = i;
mu[i] = -1;
phi[i] = i - 1;
}
for (ll j = 1; j <= cnt&&prime[j]*i<n; ++j) {
vis[prime[j] * i] = 1;
if (i % prime[j]) {
mu[i * prime[j]] = -mu[i];
phi[i * prime[j]] = phi[i] * phi[prime[j]];//积性函数性质φ(m*n)=φ(m)*φ(n)
}
else {
mu[i * prime[j]] = 0;
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
}
}
线性筛+欧拉函数+莫比乌斯函数模板
最新推荐文章于 2024-09-07 23:15:53 发布