poj 2407 Relatives(简单欧拉函数)

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

Source

 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<stdlib.h>
 7 using namespace std;
 8 #define ll long long
 9 ll eular(ll n){
10     ll ans=1;
11     for(ll i=2;i*i<=n;i++){
12         if(n%i==0){
13             ans*=i-1,n/=i;
14             while(n%i==0){
15                 ans*=i;
16                 n/=i;
17             }
18         }
19     }
20     if(n>1) ans*=n-1;
21     return ans;
22 }
23 int main()
24 {
25     ll n;
26     while(scanf("%I64d",&n)==1){
27         if(n==0) break;
28         ll ans=eular(n);
29         printf("%I64d
",ans);
30     }
31     return 0;
32 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/4771271.html