poj-1284(欧拉函数+原根)

来源:互联网 发布:java 调用执行jar包 编辑:程序博客网 时间:2024/06/05 18:32

问题描叙:

We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (x i mod p) | 1 <= i <= p-1 } is equal to { 1, ..., p-1 }. For example, the consecutive powers of 3 modulo 7 are 3, 2, 6, 4, 5, 1, and thus 3 is a primitive root modulo 7. 
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p. 

Input

Each line of the input contains an odd prime numbers p. Input is terminated by the end-of-file seperator.

Output

For each p, print a single number that gives the number of primitive roots in a single line.

Sample Input

233179
Sample Output

10824

题目题意:题目给我们一个奇素数,让我们求有多少个x满足the set { (x i mod p) | 1 <= i <= p-1 } is equal to { 1, ..., p-1 }.

题目分析:其实这个题目实质就是问p有多少个原根(哎,想要学好数论,得见多识广啊!)

下面是百度对原根的解释:

设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
假设一个数g是P的原根,那么g^i mod P的结果两两不同,且有 1<g<P, 0<i<P,归根到底就是g^(P-1) = 1 (mod P)当且仅当指数为P-1的时候成立.(这里P是素数).
简单来说,g^i mod p ≠ g^j mod p (p为素数)//这句话就是满足的条件。
其中i≠j且i, j介于1至(p-1)之间
这个题目就是将原根的定义解释了一遍。

对于这个题目来说有俩点重要的原根性质

1:m有原根的充要条件是m= 1,2,4,p,2p,p^n,其中p是奇质数,n是任意正整数

2:对正整数(a,m) = 1,如果 a 是模 m 的原根,那么 a 是整数模n乘法群(即加法群 Z/mZ的可逆元,也就是所有与 m 互素的正整数构成的等价类构成的乘法群)Zn的一个生成元。由于Zn有 φ(m)个元素,而它的生成元的个数就是它的可逆元个数,即 φ(φ(m))个,因此当模m有原根时,它有φ(φ(m))个原根。

个人实力太差了,不能给大家解释了(还得赶紧多学点才行)

我们就记住吧!模m有原根,m=1,2,4,p,2p,p^n,p是奇质数,当模m有原根时,原根的个数是φ(φ(m))(这个是欧拉函数)

因为这个题目给的n是奇素数,所以phi[n]=n-1,因为n与1到n-1都互质。

答案就是phi[n-1]

代码如下:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int get_euler(int n){    int res=n,a=n;    for (int i=2;i*i<=a;i++) {        if (a%i==0) {            res=res/i*(i-1);            while (a%i==0) a=a/i;        }    }    if (a>1) res=res/a*(a-1);    return res;}int main(){    int n;    while (scanf("%d",&n)!=EOF) {        printf("%d\n",get_euler(n-1));    }    return 0;}
































原创粉丝点击