欧拉函数
打表法
void Selcer_Euler(int n){
for(int i = 1; i <= n; i ++) phi[i] = i;
for(int i = 2; i <= n; i ++){
if(phi[i] == i){//表示i未被筛到,i是素数
for(int j = i; i <= n; j += i)
phi[j] = phi[j] / i * (i - 1);
//先除后乘防止因范围过大而溢出
}
}
}

i是i及i本身的倍数的质因数,其中
phi[j] = phi[j] / i*(i - 1)
即图片中的环节
直接计算
int euler(int x){
int ans = x,a = x;//找出所有x的质因数
for(int i = 2; i * i <= a; i ++){//从2讨论到sqrt(n)即可
if(a % i == 0){//i为x的质因数
ans = ans - ans / i;//如图
while(a % i == 0) a = a / i;//算术基本定理,抹掉i ^ 1,i ^ 2, i ^ 3 ......
}
}
if(a > 1) ans = ans - ans / a;//存在大于sqrt(a)的质因子
return ans;
}
梦里如昨,此身似我非我
冷雾割风寒浸骨,意沉南柯

浙公网安备 33010602011771号