0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

874. 筛法求欧拉函数

https://www.acwing.com/problem/content/submission/code_detail/7814036/
This commit is contained in:
Baoshuo Ren 2021-09-19 17:28:08 +08:00 committed by Baoshuo Ren
parent 4ebef12cae
commit 8dde1f3e2d
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

35
AcWing/874/874.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
int n, p, primes[1000005], phi[1000005];
bool vis[1000005];
long long ans;
void get_eulers(int n) {
phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (!vis[i]) {
primes[p++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] * i <= n; j++) {
vis[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);
}
}
}
int main() {
cin >> n;
get_eulers(n);
for (int i = 1; i <= n; i++) {
ans += phi[i];
}
cout << ans << endl;
return 0;
}