Poj1284_primitive roots [Euler's function]

Source: Internet
Author: User
Primitive rootstime limit: 1000 ms memory limit: 10000 ktotal submissions: 3032 accepted: 1734 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
Source


Jia Yi @ PKU

P is an odd prime number. If {x ^ I % p | 1 <= I <= p-1} = {1, 2 ,..., P is the original root of P.
Give a P and ask how many original roots there are.

Ideas:

{X ^ I % p | 1 <= I <= p-1} = {,..., P-1} is equivalent
{X ^ I % (p-1) | 1 <= I <= p-1} = {, 2,..., P-2 },

{X ^ 1, x ^ 2, x ^ 3 ,..., X ^ (p-1)} is equivalent to the full remainder of P.

If the interconnectivity between x and P-1 (gcd (x, P-1) = 1), then {x ^ 0, x ^ 1, x ^ 2 ,..., X ^ (P-2)} is the full residual system of (p-1 ).

Proof below:

If x ^ I! = X ^ J (mod P-1), then x * x ^ I! = X * x ^ J (mod P-1, then gcd (x, PM )! = 0, contrary to the above.

Then x ^ I = x ^ J (mod P-1 ).

According to the ferma theorem and Euler's theorem, we know that I = PHI (p-1 ).

For more information about Euler's functions, ferma's theorem, and Euler's theorem, see the question of Euler's function:

Http://blog.csdn.net/lianai911/article/details/40114675

Reference blog: http://www.cnblogs.com/lnever/p/3969117.html

By the way, worship the reasoning gods.

# Include <stdio. h> # include <math. h> int Euler (int n) {int I, ret = N; for (I = 2; I <= SQRT (1.0 * n); I ++) {If (N % I = 0) {n/= I; ret = ret-RET/I;} while (N % I = 0) N/= I ;} if (n> 1) {ret = ret-RET/N;} return ret;} int main () {int P; while (~ Scanf ("% d", & P) {printf ("% d \ n", Euler (p-1); // If P is a prime number, euler (p) = Euler (p-1)} return 0 ;}



Poj1284_primitive roots [Euler's function]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.