POJ_2407 Relatives 【欧拉函数裸题】

一、题目

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互质的数的数目。题意就是欧拉函数的定义。

三、AC代码

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = 1e5+3;
int Prime[MAXN], nPrime;
bool isPrime[MAXN];

void getPrime()
{
    memset(isPrime, 1, sizeof(isPrime));
    isPrime[0] = isPrime[1] = 0;
    nPrime = 0;
    for(int i = 2; i < MAXN; i++)
    {
        if(isPrime[i])
            Prime[nPrime++] = i;
        for(int j = 0; j < nPrime && (long long)Prime[j]*i < MAXN; j++)
        {
            isPrime[Prime[j]*i] = 0;
            if(i%Prime[j] == 0)
                break;
        }
    }
}

int Euler(int n)
{
    int ans = n;
    for(int i = 0; Prime[i]*Prime[i] <= n; i++)
    {
        if(n%Prime[i] == 0)
        {
            ans = ans - ans/Prime[i];
            do
            {
                n/=Prime[i];
            }
            while(n%Prime[i] == 0);
        }
    }
    if(n > 1)
        ans = ans - ans/n;
    return ans;
}

int main()
{
    int N;
    getPrime();
    while(cin >> N && N)
    {
        cout << Euler(N) << endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/dybala21/p/9747397.html

时间: 2024-07-31 22:46:52

POJ_2407 Relatives 【欧拉函数裸题】的相关文章

POJ 2407 Relatives(欧拉函数入门题)

Relatives 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 t

[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

(hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 166 Accepted Submission(s): 96   Problem Description The Euler function phi is an important kind of function in number theory

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的所有质因数 就是要求这样的式子啦,不过求这条式子,相信有很多种方法可以求,这个不是难题: 不过问题是如何巧妙地求,如何简洁地写出代码. 直接硬求,或者求出质因数之后求都不是巧妙的方法了,参考了下别人的代码才知道可以写的这么巧妙的. 下面程序可以说是连消带打地求式子结果,分解质因子,可以如此简明地把解

poj2478 欧拉函数水题

poj2478 欧拉函数水题 Y - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2478 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational number

hdu2824 The Euler function 筛选法求欧拉函数模板题

//求a , b范围内的所有的欧拉函数 //筛选法求欧拉函数模板题 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 3000010 ; typedef __int64 ll ; int e[maxn] ; int a ,  b ; void Euler() { int i,j; for (i=1;i<maxn;i++) e[i]

poj2407(欧拉函数模板题)

题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p2,...,pn,然后根据Φ(m)=m*∏(1-1/pi)计算即可. AC代码: #include<cstdio> using namespace std; int n,ans; int main(){ while(scanf("%d",&n),n){ ans=n; f

数论 - 欧拉函数模板题 --- poj 2407 : Relatives

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 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 ther

【BZOJ4173】数学 欧拉函数神题

[BZOJ4173]数学 Description Input 输入文件的第一行输入两个正整数 . Output 如题 Sample Input 5 6 Sample Output 240 HINT N,M<=10^15 题解:STEP 1: 这步还是很容易的吧~毕竟原来的式子不太舒服.但是注意,最后一个式子的取值只能为0或1,所以就变成了. STEP 2: 这步倒是难理解一些,但是考虑:我们将这三个等式都算出来,如果满足了左边那个条件,那么这三个等式加起来为1,对答案的贡献正好为$\varphi