前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2407Relatives

POJ 2407Relatives

作者头像
attack
发布2018-04-11 13:47:02
6310
发布2018-04-11 13:47:02
举报

Description

Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

Input

There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output

For each test case there should be single line of output answering the question posed above.

Sample Input

代码语言:javascript
复制
7
12
0

Sample Output

代码语言:javascript
复制
6
4

Source

Waterloo local 2002.07.01

题目大意

给定n,求出\varphi \left( n\right)

直接套公式。

\varphi \left( n\right) =n\prod ^{k}_{i=1}\left( \dfrac {p_{i}-1}{p_{i}}\right)

注意先除再乘,否则会爆精度

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long 
using namespace std;
int main()
{
    LL N;
    while(cin>>N&&N!=0)
    {
        int limit=sqrt(N),ans=N;
        for(int i = 2; i <= limit ; ++i)
        {
            if(N%i==0) ans=ans/i*(i-1);
            while(N%i==0) N=N/i;
        }
        if(N>1) ans=ans/N*(N-1);
        printf("%d\n",ans);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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