码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-19 09:43:17      阅读:161      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!