线性筛
public static void Euler() {
count = 0;
for (int i = 2; i <= N; i += 1) {
if (!visited[i]) {
prime[count] = i;
count += 1;
}
for (int j = 0; prime[j] * i <= N; j += 1) {
visited[prime[j] * i] = true;
if (i % prime[j] == 0) {
break;
}
}
}
}
欧拉函数筛(求1-n)
public static void get_phi() {
phi[1] = 1;
count = 1;
for (int i = 2; i <= n; i += 1) {
if (!visited[i]) {
prime[count] = i;
phi[i] = i - 1;
count += 1;
}
for (int j = 1; j < count && prime[j] * i <= n; j += 1) {
visited[i * prime[j]] = true;
if (i % prime[j] == 0) {
phi[i * prime[j]] = prime[j] * phi[i];
break;
} else {
phi[i * prime[j]] = (prime[j] - 1) * phi[i];
}
}
}
}
求n的欧拉函数
图源蓝桥杯题解区