首页 > 其他 > 详细

POJ 2407 Relatives && UVA 10299 Relatives(欧拉函数)

时间:2015-09-19 09:42:09      阅读:175      评论:0      收藏:0      [点我收藏+]

【题目链接】:click here~~

【题目大意】:欧拉函数:求少于或等于n的数中与n互素的数的个数;n <= 1,000,000,000。

【思路】:裸欧拉函数,注意特判n==1的情况,n==1的情况下,应该输出0,poj依然判断1也可以过,但是老牌ojUVA必须是0才过,注意一下。

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

int eular(int n){
    int res=n;
    for(int i=2; i*i<=n; ++i){
        if(n%i==0){
            n/=i;res=res-res/i;
            while(n%i==0)
            {
                n/=i;
            }
        }
    }
    if(n!=1) res=res-res/n;
    return res;
}
int main()
{
    int n;while(cin>>n&&n!=0)
    {
        if(n==1) puts("0");
        else printf("%d\n",eular(n));
    } return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2407 Relatives && UVA 10299 Relatives(欧拉函数)

原文:http://blog.csdn.net/u013050857/article/details/48572115

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!