码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2407 Relatives(简单欧拉函数)

时间:2015-08-30 19:13:59      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

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\n",ans);
30     }
31     return 0;
32 }
View Code

 

poj 2407 Relatives(简单欧拉函数)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4771271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!