POJ1284 Primitive Roots - 数论 - 原根 - 欧拉函数

Primitive Roots

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6187   Accepted: 3529

Description

We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (xi 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

 
 

题目大概意思:

给出一个素数 p ( 3 p &lt; 65536 ) p(3≤p&lt;65536) ,求模 p p 原根的个数。

 
 

分析:

根据以下两个定理即可解决此题:

  1. m m 有原根的充要条件是 m = 2 , 4 , p a , 2 p a m=2,4,p^a,2p^a ,其中 p p 是奇素数, a a 是任意正整数。
  2. 当模 m m 有原根时,它有 ϕ ( ϕ ( m ) ) \phi(\phi(m)) 个原根。

(其中 ϕ ( x ) \phi(x) 欧拉函数

扫描二维码关注公众号,回复: 11164623 查看本文章

 
由于 p p 为大于 2 2 的素数,因此一定有原根。我们预处理出 [ 1 , 65536 ] [1,65536] 的欧拉函数值的表,再根据定理计算答案即可。

 
 
下面贴代码:

#include <cstdio>
using namespace std;

const int MAX_N = 65536;

int euler[MAX_N];
void euler_phi();

int main()
{
	int p;
	euler_phi();
	while (~scanf("%d", &p))
	{
		printf("%d\n", euler[euler[p]]);
	}
	return 0;
}

void euler_phi()
{
	for (int i = 1; i < MAX_N; ++i)
	{
		euler[i] = i;
	}
	for (int i = 2; i < MAX_N; ++i)
	{
		if (euler[i] == i)
		{
			for (int j = i; j < MAX_N; j += i)
			{
				euler[j] = euler[j] / i * (i - 1);
			}
		}
	}
}

原创文章 42 获赞 22 访问量 3035

猜你喜欢

转载自blog.csdn.net/weixin_44327262/article/details/98629010
今日推荐