[Ybt gold medal navigation 8-6-4] the number of original roots

Number of original roots

Topic link: ybt gold medal navigation 8-6-4

General idea

Give you an odd prime number p, and ask you the number of its primitive roots.

Ideas

First, we need to know what the original root is.

Order

Before talking about the primitive root, we need to know what the order is.

Let n> 1 n>1n>1 a a a is the same asnnn is a relatively prime number, then according to the extended Euclid, we can know that there must be manyrrr makesar ≡ 1 (mod n) a^r\equiv 1(mod\ n)ar1 ( m o d n )  . And the smallest one will be between1 ∼ n 1\sim n1Between n , this value is calledaaa modennThe order of n is denoted asO rdn (a) Ord_n(a)O r dn(a)

It should be noted that after gcd ⁡ (a, n) = 1 \gcd(a,n)=1gcd(a,n)=When it is 1 , there will be an order, otherwise its equation will have no solution, that is, there will be no order.

Then let's talk about some of its properties: (We assume that we already know gcd ⁡ (a, n) = 1 \gcd(a,n)=1gcd(a,n)=1 )
Knownaaa modennThe order of n isrrr , if there is a numberNNN makesa N ≡ 1 (mod n) a^N\equiv1(mod\ n)aN1 ( m o d n )  , thenNNN isrrThe multiple of r , which isr ∣ N r|NrN

After that aaa modennorder of n rrr it is divisible byφ (n) \varphi(n)φ ( n ) . Then ifnnn is a prime number, thenrrr it dividesn − 1 n-1n1 . (Because theφ \varphi ofprime numbersThe value of φ is minus one)
(This is obtained by Euler's theorem)

Euler's theorem: for mutually prime positive integers a and n, there is a^φ(n) ≡ 1(mod n)

Then there is another one, remember n = ordm (a) n=ord_m(a)n=o r dm( a ) , for greater than0 00 basisiii,如果 o r d m ( a i ) = s ord_m(a^i)=s o r dm(ai)=s , thens = n gcd ⁡ (n, i) s=\dfrac{n}{\gcd(n,i)}s=gcd(n,i)n

Primitive root

Then we look at what the original root is.
The primitive root is to satisfy O rdm (a) = φ (m) Ord_m(a)=\varphi(m)O r dm(a)=[Phi] ( m ) ofaaa , calledaaa ismmThe primitive root of m .
Then you can see that it is modmmm remaining class, then we only look at it at0 ∼ m 0\sim m0m 's solution.

Then it also has some properties.

  1. 首先, a 0 , a 1 , a 2 , . . . , a O r d m ( a ) − 1 a^0,a^1,a^2,...,a^{Ord_m(a)-1} a0,a1,a2,...,aO r dm( a ) 1 modemmm is definitely different. Thatdang aaa is modulusmmWhen the original root of m , the above must constitute the modulusmmm simple residual system. Then when the moldmm isformed on itWhen the simple residual system of m , then thisaaa is the modulusmmThe primitive root of m .
    One special thing is whenmmWhen m is a prime number, theaa on itWhen a is modulo its primitive root, it not only constitutes a simple residual system, but it is also like this:1, 2,..., m − 1 1,2,...,m-11,2,...,m1
  2. All prime numbers have primitive roots.
  3. Not all integers have primitive roots.
  4. When a few mmWhen m has primitive roots, it hasφ (φ (m)) \varphi(\varphi(m))φ ( φ ( m ) ) primitive roots.
    This can be explained in terms of the last property of the order.

This question

This question is to directly use the fourth nature of the original root, just fine.

Code

#include<cstdio>

using namespace std;

int p;

int phi(int now) {
    
    //求phi值
	int re = now;
	for (int i = 2; i * i <= now; i++)
		if (now % i == 0) {
    
    
			re = re / i * (i - 1);
			while (now % i == 0) now /= i;
		}
	if (now > 1) re = re / now * (now - 1);
	return re;
}

int main() {
    
    
	while (scanf("%d", &p) != EOF) {
    
    
		printf("%d\n", phi(p - 1));//phi(phi(p)),而质数的 phi 值是它减一,所以就省去了一次
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/114198893