欧拉函数的定义
1∼N1∼N 中与 NN 互质的数的个数被称为欧拉函数,记为 ϕ(N)ϕ(N)。
若在算数基本定理中,N=pa11pa22…pammN=p1a1p2a2…pmam,则:
ϕ(N)ϕ(N) = N×p1−1p1×p2−1p2×…×pm−1pm
代码:
#include<iostream>
using namespace std;
int n;
int main(){
scanf("%d",&n);
while(n--){
int x;
scanf("%d",&x);
int res=x;
for(int i=2;i<=x/i;i++){
if(x%i==0){
res =res/i * (i-1);
while(x%i==0) x/=i;
}
}
if(x>1) res = res/x * (x-1);
printf("%d\n",res);
}
}