欧拉函数是指:一个数N,在1~N这个范围内,与N互质的数的“个数”记作 \(\phi\)(N) 所以我们可以得到\(\phi\)(N) = \(\phi\)(P\(_{1}\)\(^{a1}\))*\(\phi\)(P\(_{2}\)\(^{a2}\))*\(\phi\)(P\(_{3}\)\(^{a3}\))*......*\(\phi\)(P\(_{k}\)\(^{ak}\)) \(\phi\)(N) = N * (1-\(\frac{1}{a1}\))*(1-\(\frac{1}{a2}\))*.....*(1-\(\frac{1}{ak}\)) 2.线性筛法定义:
互质是指gcd(i,N) = 1
因为一个数总能被分解为:N = P\(_{1}\)\(^{a1}\)*P\(_{2}\)\(^{a2}\)*P\(_{3}\)\(^{a3}\)*....*P\(_{k}\)\(^{ak}\)
且欧拉函数为一个积性函数:\(\phi\)(mn) = \(\phi\)(m) * \(\phi\)(n)
对于其中的某一项\(\phi\)(P\(_{i}\)\(^{ai}\))分析:从1到P\(_{i}\)\(^{ai}\)共有P\(_{i}\)\(^{ai}\)个数,其中与P\(_{i}\)\(^{ai}\)不互质的有P\(_{i}\),2*P\(_{i}\),3*P\(_{i}\).....P\(_{i}\)\(^{ai-1}\)*P\(_{i}\)共有P\(_{i}\)\(^{ai-1}\)项,所以互质的就有P\(_{i}\)\(^{ai}\)-P\(_{i}\)\(^{ai-1}\)项,所以我们可以得到公式代码实现:
void solve() {
int x;
cin>>x;
int res = x;
for(int i = 2;i <= x/i;++i){
if(x%i == 0){
while(x%i == 0) x/= i;
res = res/i*(i-1);
}
}
if(x>1) res = res/x*(x-1);
cout<<res<<endl;
}
以上解释引用自Sundaeint primes[N],cnt;//线性筛
int phi[N];//欧拉函数
bool st[N];
void 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] = primes[j]*phi[i];
break;
}
phi[primes[j]*i] = phi[i]*(primes[j]-1);
}
}
}