欧拉函数φ(N)表示小于或等于N的正整数中与N互质的数的个数。又称φ函数、欧拉商数。
下面介绍欧拉函数的几个性质:
我们根据这几个性质就可以求出欧拉函数。
基本思路是首先置φ(N)=N,然后再枚举素数p,将p的整数倍的欧拉函数φ(kp)进行如下操作。
例题
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input
7 12 0
Sample Output
6 4
#include<iostream>
#include<cstdio>
typedef long long ll;
using namespace std;
ll f(ll a){
ll ans=1;
for(int i=2;i*i<=a;i++){
if(a%i==0){
a/=i;ans*=i-1;
while(a%i==0){ //用来求幂
a/=i;ans*=i;
}
}
}
if(a>1) ans*=a-1; //如果本身是质数那么质数-1就是结果
return ans;
}
int main()
{
ll n;
while(cin>>n){
if(n==0) break;
cout<<f(n)<<endl;
}
return 0;
}