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;
        for(int i=2;i*i<=n;++i){
            if(n%i==0){
                ans=ans/i*(i-1);
                while(n%i==0) n/=i;
            }
        }
        if(n!=1)
            ans=ans/n*(n-1);
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/FrankChen831X/p/10824683.html

时间: 2024-10-05 03:50:06

poj2407(欧拉函数模板题)的相关文章

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]

(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

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

hdu1286找新朋友 欧拉函数模板题

#include<cstdio> #include<cstring> #include<iostream> using namespace std ; int Euler(int n) { int rea = n ; for(int i = 2;i*i <= n;i++) { if(n%i == 0) rea -= rea/i ; while(n%i == 0) n/=i ; } if(n>1) rea-=rea/n ; return rea ; } int

POJ 1284 Primitive Roots 欧拉函数模板题

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io

HDU 1286 找新朋友(欧拉函数模板)

HDU 1286 找新朋友 题意:中文题. 思路:欧拉函数的纯模板题,没什么好说的,主要是理解欧拉函数的意义. 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等. 例如φ(8)=4,因为1,3,5,7均和8互质.   ----by度娘. #include <stdio.h> int eular(int n){ int ret = 1; for(int i = 2; i*

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

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

hdu 1787(欧拉函数+水题)

题意:给出一个n,求小于n大于0的所有与n不互质的数的个数 是一道欧拉函数的模板题 1 #include<iostream> 2 #include<string.h> 3 #include<string> 4 #include<sstream> 5 #include<vector> 6 #include<deque> 7 #include<map> 8 #include<algorithm> 9 #includ