线性筛质数
const int N = 1000001;
int primes[N], cnt;
bool st[N];
void init() {
for (int i = 2; i < N; ++i) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; i * primes[j] < N; ++j) {
st[i * primes[j]] = true;
if (i % primes[j] == 0) break;
}
}
}
筛法求 $\varphi(x)$
int phi[N];
void init() {
for (int i = 2; i < N; ++i) {
if (!st[i]) primes[cnt++] = i, phi[i] = i - 1;
for (int j = 0; i * primes[j] < N; ++j) {
st[i * primes[j]] = true;
if (i % primes[j]) {
phi[i * primes[j]] = phi[i] * (primes[j] - 1);
} else {
phi[i * primes[j]] = phi[i] * primes[j];
break;
}
}
}
}
筛法求 $\mu(x)$
int mu[N];
void init() {
mu[1] = 1; // 这里一定要记得手动初始化 mu[1]!!QAQ
for (int i = 2; i < N; ++i) {
if (!st[i]) primes[cnt++] = i, mu[i] = -1;
for (int j = 0; i * primes[j] < N; ++j) {
st[i * primes[j]] = true;
if (i % primes[j] == 0) break;
mu[i * primes[j]] = -mu[i];
}
}
}
筛法求 $d(i) = \sum _ {k | x} 1$
int d[N], g[N];
void init() {
d[1] = 1;
for (int i = 2; i < N; ++i) {
if (!st[i]) primes[cnt++] = i, d[i] = 2, g[i] = 2;
for (int j = 0; i * primes[j] < N; ++j) {
st[i * primes[j]] = true;
if (i % primes[j]) {
g[i * primes[j]] = 2;
d[i * primes[j]] = 2 * d[i];
} else {
g[i * primes[j]] = g[i] + 1;
d[i * primes[j]] = d[i] / g[i] * (g[i] + 1);
break;
}
}
}
}
筛法求 $w(x) = \sum _ {k | x} k$
int w[N], g[N];
void init() {
w[1] = 1;
for (int i = 2; i < N; ++i) {
if (!st[i]) primes[cnt++] = i, w[i] = g[i] = i + 1;
for (int j = 0; i * primes[j] < N; ++j) {
st[i * primes[j]] = true;
if (i % primes[j]) {
g[i * primes[j]] = primes[j] + 1;
w[i * primes[j]] = w[i] * (primes[j] + 1);
} else {
g[i * primes[j]] = g[i] * primes[j] + 1;
w[i * primes[j]] = w[i] / g[i] * g[i * primes[j]];
break;
}
}
}
}
其它的忘了=,=
stO %%% Orz
原来百科上的是线性筛
tql 虽然我看不懂 但是还是收藏了
请问 phi_x 这些是在哪里定义的呢
$\varphi(x)$ 是欧拉函数{:target=”_blank”},$\mu(x)$ 是莫比乌斯函数{:target=”_blank”}
嗯嗯👍