POJ 1284 Primitive Roots (原根)

Primitive Roots
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3219   Accepted: 1858

Description

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

23
31
79

Sample Output

10
8
24

这是一道关于原根的题目,先翻译一下题目,这样可以更好的了解原根:一个整数x(0<x<p) 是奇素数p的原根,当且仅当集合
{ ( x^i mod p ) | 1<= i <= p-1} 与集合 {1,2,3,...,p-1}是相同的。    例如,3的连续次幂对7取模的结果是3,2,6,4,5,1,所以3是7的一个原根。

知识:
求原根的方法有两种:一,随机生个一个数g,检验其是否是原根;二,一般来说,最小正原根往往比较小,所以可以采用从小到大尝试的方法。

这个题是给一个素数p,求p的原根的个数。
给出一个结论:p是素数,则p有phi(p-1)个原根,其中phi为欧拉函数。

#include <stdio.h>
#include <string.h>

const int maxp=65536;
int phi[maxp+10];

void phi_table(){
	int i,j;
	memset(phi,0,sizeof(phi));
	phi[1]=1;
	for(i=2;i<=maxp;i++) if(!phi[i])
		for(j=i;j<=maxp;j+=i){
			if(!phi[j]) phi[j]=j;
			phi[j]=phi[j]/i*(i-1);
		}
}

int main()
{
	int p;
	phi_table();
	while(scanf("%d",&p)!=EOF)
		printf("%d\n",phi[p-1]);
	return 0;
}


你可能感兴趣的:(数论,poj)