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 <iomanip>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007

using namespace std;

int p;
unsigned euler(unsigned x)
{
    unsigned i,res=x;
    for(i=2;i<(int)sqrt(x*1.0)+1;i++)
        if(x%i==0)
        {
            res=res/i*(i-1);
            while(x % i==0)
                x/=i;
        }
    if(x>1)
        res=res/x*(x-1);
    return res;
}
int main()
{
    while(scanf("%d",&p)!=EOF)
    {
        printf("%d\n",euler(p-1));
    }
    return 0;
}

时间: 2024-10-12 16:38:14

POJ 1284 Primitive Roots 欧拉函数模板题的相关文章

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

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

【poj1284】Primitive Roots 欧拉函数

题目描述: 题意: 定义原根:对于一个整数x,0<x<p,是一个mod p下的原根,当且仅当集合{ (xi mod p) | 1 <= i <= p-1 } 等于{ 1, ..., p-1 }.给定p,询问有多少个满足定义的原根. 这里有一个定理:如果p有原根,则它恰有φ(φ(p))个不同的原根 证明不懂就算了,我也不懂啊TAT 证明如下 题目中说m是奇素数,所以φ(p)=p-1,故ans=φ(p-1). 代码如下: 1 #include<cstdio> 2 #incl

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 【原根】【数论】

题目链接 :传送门 题目大意: 求一个质数的原根个数. 先普及一下原根的定义: 设m是正整数,a是整数,若a模m的阶等于euler(m),则称a为模m的一个原根. eg: m=7,euler(7) =  6(1,2,3,4,5,6) 则: 1   1^(n)mod7=1! = 6 2   2^(n)mod7={2 4 1}!=6 3   3^(n)mod7={3,2,6,4,5,1}==6   故3是模7的原根 4   4^(n)mod7={4,2,1}!=6 5   5^(n)mod7={5,

POJ 1284 Primitive Roots (求原根个数)

Primitive Roots 题目链接:http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) typedef long long ll; using namespace std; /* 求原根 g^d ≡ 1(mod p) 其中d最小为p-1,g 便是一个原根 复杂度:O(m)*log(P-1)(m为p-1的质因子个数) */ ll euler(ll x) { ll res = x; for (ll i = 2; i <= x