POJ 2407-Relatives(求一个整数的欧拉函数值)

Relatives

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
2407

Appoint description: 
System Crawler  (2015-04-04)

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

题意:给一个数n,求出不大于n且与n互素的数的个数。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);

int Euler(int n)
{
    int m=floor(sqrt(n+0.5));
    int ans=n;
    for(int i=2;i<=m;i++){
        if(n%i==0){
            ans=ans/i*(i-1);
            while(n%i==0){
                n/=i;
            }
        }
    }
    if(n>1)
        ans=ans/n*(n-1);
    return ans;
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        if(!n) break;
        printf("%d\n",Euler(n));
    }
    return 0;
}
时间: 2024-08-27 04:48:01

POJ 2407-Relatives(求一个整数的欧拉函数值)的相关文章

POJ 2407 Relatives 欧拉函数题解

最基本的欧拉函数: 欧拉函数:求小于n的与n互质的个数 欧兰函数公式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)-..(1-1/pn),其中p1, p2--pn为x的所有质因数 就是要求这样的式子啦,不过求这条式子,相信有很多种方法可以求,这个不是难题: 不过问题是如何巧妙地求,如何简洁地写出代码. 直接硬求,或者求出质因数之后求都不是巧妙的方法了,参考了下别人的代码才知道可以写的这么巧妙的. 下面程序可以说是连消带打地求式子结果,分解质因子,可以如此简明地把解

[POJ 2407]Relatives(欧拉函数)

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

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

求一个整数的位数

C++ 吧里看到有人求助, 说是想写点代码求一个整数的位数, 以下是我的解决方法: int32_t RecursionIter (const int64_t& num, const int32_t& result) { if (num < 0) { return RecursionIter (-num, result); } else if (num / 10 == 0) { return result; } else { return RecursionIter (num / 10

求一个整数的最大质因数(回答一个群友的问题)

[QQ群: 189191838,对算法和C++感兴趣可以进来] 群里最近每天都聊得很嗨,虽然不一定是技术,更多的是生活,还是非常开心.因为工作.程序只是我们生命中的一小部分,人的幸福与否,更多的由人的业余生活和他的精神世界所决定.和一群陌生的人,扯扯淡,毫无压力的聊聊人生觉得也挺好. 好的,直接奔入主题.情况是这样的,群里有位同学问了一道算法,求一个整数的最大质因数.然后他给出了代码(如下所示),但是测试没有通过. 1 int largest_prime_factor(int n) 2 { 3

求一个整数的二进制中1的个数

题目:输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. 假设该整数为i.首先i和1做与运算,判断i的最低位是不是为1.接着把1左移一位得到2,再和i做与运算,就能判断i的次高位是不是1……这样反复左移,每次都能判断i的其中一位是不是1.基于此,我们得到如下代码 int NumberOf1_Solution(int i) { int count = 0; unsigned int flag = 1; while(flag) { if(

高效求一个整数中1的位数

求一个整数中0或1的位数,有很多方法可以使用除法,求余等方法,也可以使用位运算,相比前者效率更高. #include <stdio.h> #include <stdlib.h> //求一个整数 1的位数 int count0(int x) { int num=0; while(x) { num+=x%2; x/=2; } return num; } int count1(int x) { int num=0; while(x) { num+=(x&0x01); x>&

求一个整数中二进制1的个数

题目:求一个整数二进制表示1的个数 第一版: 思路:如果一个整数与1做与运算,结果为1,那么该整数最右边一位是1,否则是0: int NumberOf1(int n) { int count = 0; while (n) { if (n&1)//如果一个整数与1做与运算的结果是1,表示该整数最右边是1,否则是0: { count++; } n = n>>1; } return count; } 缺点:因为代码当中有右移,当是负数的时候,要考虑符号位:如果一个正数,右移之后在最左边补n个

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th