首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ACMSGURU 102 - Coprimes

ACMSGURU 102 - Coprimes

作者头像
Reck Zhang
发布2021-08-11 11:10:02
发布2021-08-11 11:10:02
4120
举报
文章被收录于专栏:Reck ZhangReck Zhang

Coprimes

Problem Description

For given integer N (1<=N<=10^4) 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

代码语言:javascript
复制
9

Sample Output

代码语言:javascript
复制
6

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    int n;
    std::cin >> n;

    int ans = n;
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            ans -= ans / i;
            while(n % i == 0) {
                n /= i;
            }
        }
    }

    if(n > 1) {
        ans -= ans / n;
    }

    std::cout << ans << std::endl;

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Coprimes
    • Problem Description
    • Input
    • Output
    • Sample Input
    • Sample Output
    • Solution
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档