POJ-2407 Relatives 欧拉phi函数

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

  欧拉函数模板题目:

    phi(n)=n*(1-1/p1)*(1-1/p1)*...*(1-1/pn)。

 1 //STATUS:C++_AC_0MS_152KB
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<math.h>
 6 #include<iostream>
 7 #include<string>
 8 #include<algorithm>
 9 #include<vector>
10 #include<queue>
11 #include<stack>
12 #include<map>
13 using namespace std;
14 #define LL __int64
15 #define pii pair<int,int>
16 #define Max(a,b) ((a)>(b)?(a):(b))
17 #define Min(a,b) ((a)<(b)?(a):(b))
18 #define mem(a,b) memset(a,b,sizeof(a))
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N=11,INF=0x3f3f3f3f,MOD=10000,STA=8000010;
22 const double DNF=1e13;
23 
24 int n;
25 
26 LL phi(LL n)
27 {
28     int i,j;
29     LL m,ans=n;
30     m=(LL)sqrt(n+0.5);
31     for(i=2;i<m && i<n;i++){
32         if(n%i==0){
33             ans=ans/i*(i-1);
34             while(n%i==0)n/=i;
35         }
36     }
37     if(n>1)ans=ans/n*(n-1);
38     return ans;
39 }
40 
41 int main()
42 {
43  //   freopen("in.txt","r",stdin);
44     LL n;
45     while(~scanf("%I64d",&n) && n)
46     {
47         printf("%I64d\n",phi(n));
48     }
49     return 0;
50 }

 

posted @ 2013-04-13 13:07  zhsl  阅读(209)  评论(0编辑  收藏  举报