• 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
  • 相关阅读:
    SpringMVC数据绑定
    SpringMVC概述
    应用Spring和Hibernate(C3P0数据池)写数据库交互项目
    Spring的AOP特性
    Sping框架的IOC特性
    MD5加密字符串
    重力感应 视频横竖屏切换
    自定义View(三)实现简单的可拖动、可缩放的ImageView
    自定义View(二)增加View的属性
    自定义View的学习(一) 自绘制控件
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3581273.html
Copyright © 2020-2023  润新知