SGU 102

For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1).

Input

Input file contains integer N.

Output

Write answer in output file.

Sample Input

9

Sample Output

6


首先找出n的质因子,然后走一次o(n)找出所有不互质的数,减去即可,注意1与自身互质。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define maxn 10005
 8 
 9 int n,len = 0;
10 int ele[20];
11 bool prime[maxn],vis[maxn];
12 
13 void solve() {
14     for(int i = 3; i <= n; i++) {
15             prime[i] = i % 2;
16     }
17 
18     prime[2] = 1;
19     int ans = n;
20     for(int i = 2; i < n; i++) {
21             if(prime[i]) {
22                     if(n % i == 0) {
23                             ele[len++] = i;
24                     }
25                     for(int j = i; j * i <= n; j++) {
26                             prime[j * i] = 0;
27                     }
28 
29             }
30     }
31 
32     vis[1] = 1;
33     int sum = 0;
34     for(int i = 0; i < len; i++) {
35             for(int j = 2; j < n; j++) {
36 
37                     if(j % ele[i] == 0) vis[j] = 1;
38             }
39     }
40 
41     for(int j = 2; j < n; j++) if(vis[j]) sum++;
42     sum++;
43     if(prime[n]) printf("%d\n",n - 1);
44     else
45     printf("%d\n",ans - sum);
46 
47 }
48 int main()
49 {
50 
51     scanf("%d",&n);
52     
53     if(n == 1) printf("1\n");
54     else solve();
55 
56 
57     //cout << "Hello world!" << endl;
58     return 0;
59 }
View Code

 

posted @ 2014-03-04 22:01  hyx1  阅读(217)  评论(0编辑  收藏  举报