POJ2407:Relatives(欧拉函数)

Relatives
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9411   Accepted: 4453

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

7
12
0

Sample Output

6
4

Source

MYCode:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int euler(int x)
{
    int res=x;
    int i;
    for(i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            res=res*1.0/i*(i-1);
            while(x%i==0)x/=i;
        }
    }
    if(x>1)res=res/x*(x-1);
    return res;
}
int main()
{
    int p;
    while(scanf("%d",&p))
    {
        if(p==0)
        break;
        int ans=euler(p);
        printf("%d\n",ans);
    }
}
//
欧拉函数模板题,可以使用素数表优化.
posted @   java程序员-c  阅读(131)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示