Poj.2407.Relatives

A - Relatives

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status]

Description

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<cmath>
using namespace std;

int phi(int n){
    int ans=n;
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0){
            ans=ans/i*(i-1);
            while(n%i==0)n/=i;
        }
    }
    if(n>1)ans=ans/n*(n-1);
    return ans;
}
int main()
{
    int n;
    while(cin>>n&&n){
        int ans=phi(n);
        cout<<ans<<endl;
    }
}


猜你喜欢

转载自blog.csdn.net/xxxxxm1/article/details/80291977