zoukankan      html  css  js  c++  java
  • 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 - 1);
    44     else
    45     printf("%d
    ",ans - sum);
    46 
    47 }
    48 int main()
    49 {
    50 
    51     scanf("%d",&n);
    52     
    53     if(n == 1) printf("1
    ");
    54     else solve();
    55 
    56 
    57     //cout << "Hello world!" << endl;
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    努力
    散步
    相信自己
    我仅有的倔强
    存储过程 有用
    面试题整理 !=!=未看 *****面试题整理最全 有用
    项目介绍4 y有用
    面试题;40个多线程的问题 背1 有用
    面试题: redis面试题 有用 redis详细
    数据库相关内容 已看1 有用
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3581273.html
Copyright © 2011-2022 走看看