POJ 2407 Relatives(欧拉函数模板)

版权声明:欢迎加好友讨论~ QQ1336347798 https://blog.csdn.net/henu_xujiu/article/details/81457606

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>
#include <cstring>
using namespace std;

int euler(int n) {
    int rea=n;
    for(int i = 2;i*i <= n;i++) 
	{
        if(n%i == 0) 
		{
            rea = rea - rea/i;
            while(n%i==0) 
			{
				 n = n/i; 
			}
        }
    }
    if(n>1) 
	{
		 rea=rea-rea/n;
	}  
    return rea;
}
 
int main()
{
    int n;
    while(cin>>n) 
	{
    	if(n==0)break;
        int res = euler(n);
        printf("%d\n", res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81457606