poj2407 Relatives ——欧拉函数入门题

题目链接:http://poj.org/problem?id=2407

题目大意:

  这个题目就是欧拉函数的定义,求一个数字的欧拉函数。

题目思路:

  用公式:φ(n) = n * (1-1/p1) * (1-1/p2) * ……  * (1-1/pk)。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cctype>
 6 #include <stack>
 7 #include <queue>
 8 #include <map>
 9 #include <set>
10 #include <vector>
11 #include <cmath>
12 #include <algorithm>
13 #define lson l, m, rt<<1
14 #define rson m+1, r, rt<<1|1
15 using namespace std;
16 typedef long long int LL;
17 const int MAXN =  0x3f3f3f3f;
18 const int  MIN =  -0x3f3f3f3f;
19 const double eps = 1e-9;
20 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
21   {1,1},{1,-1},{-1,-1}};
22 
23 int main(void){
24 #ifndef ONLINE_JUDGE
25   freopen("poj2407.in", "r", stdin);
26 #endif
27   LL n;
28   while (~scanf("%lld", &n)){
29     if (!n) break;
30     LL ans = n;
31     for (LL i = 2; i * i <= n; ++i){
32       if (!(n%i)){
33         ans = ans - ans / i;
34         while (!(n%i)){
35           n /= i;
36         }
37       }
38     }
39     if (n > 1) { ans = ans - ans / n; }
40     printf("%lld\n", ans);
41   }
42 
43   return 0;
44 }

  写代码要规范,优先级什么的,比如刚开始就写成了 !n%i这是不对的,优先级搞错了,应该加括号。

 

posted on 2013-04-22 08:36  aries__liu  阅读(186)  评论(0编辑  收藏  举报