【欧拉函数】AcWing874.筛法求欧拉函数——筛法求欧拉函数
作者:互联网
AcWing874.筛法求欧拉函数
题解
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int primes[N], eulars[N], cnt;
bool st[N];
void get_eulars(int n)
{
eulars[1] = 1;
for(int i = 2; i <= n; ++i)
{
if(!st[i])
{
primes[cnt ++ ] = i;
eulars[i] = i - 1;
}
for(int j = 0; primes[j] <= n / i; ++j)
{
int t = i * primes[j];
st[t] = true;
if(i % primes[j] == 0)
{
eulars[t] = eulars[i] * primes[j];
break;
}
eulars[t] = eulars[i] * (primes[j] - 1);
}
}
}
int main()
{
int n;
cin >> n;
get_eulars(n);
LL res = 0;
for(int i = 1; i <= n; ++i)
res += (LL)eulars[i];
cout << res << endl;
return 0;
}
标签:eulars,函数,筛法,int,LL,欧拉 来源: https://www.cnblogs.com/czy-algorithm/p/16353851.html