AcWing 874. 筛法求欧拉函数
原题链接
简单
作者:
wugensheng
,
2021-04-12 23:21:57
,
所有人可见
,
阅读 188
线性筛法求欧拉函数-y总🐂
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
typedef long long LL;
int primes[N], cnt;
int phi[N];
int st[N];
LL get_eulers(int n) {
phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (!st[i]) {
primes[cnt++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) {
phi[primes[j] * i] = phi[i] * primes[j];
break;
}
phi[primes[j] * i] = phi[i] * (primes[j] - 1);
}
}
LL res = 0;
for (int i = 1; i <= n; i++) {
res += phi[i];
}
return res;
}
int main() {
int n;
cin >> n;
cout << get_eulers(n) << endl;
return 0;
}