筛法求欧拉函数
作者:
ICE_TEA
,
2023-02-06 11:09:36
,
所有人可见
,
阅读 169
#include<bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
const int nn = 1e6 + 5;
bool t[nn];
int phi[nn];
vector<int> p;
signed main()
{
phi[1] = 1;
int n;
cin >> n;
int aw = 0;
for (int i = 2; i <= n; i++)
{
if (!t[i])
{
phi[i] = i - 1;
p.push_back(i);
}
for (int j = 0; p[j] <= n / i; j++)
{
t[p[j] * i] = true;
if (i % p[j] == 0)
{
phi[p[j] * i] = phi[i] * p[j];
break;
}
phi[p[j] * i] = phi[i] * (p[j] - 1);
}
}
for (int i = 1; i <= n; i++)
aw += phi[i];
cout << aw;
}