51nod 1135 Original Root

Source: Internet
Author: User

Title Link: 51nod 1135 original Root

Set M is a positive integer, A is an integer, if the order of a-mode m is equal to φ (m), then A is called a primary root of modulo m. (where φ (m) represents the Euler function of M)

Order: gcd (a,m) = 1, which makes the smallest restablished, called A-to-modulo m-order .

φ (m): The number of coprime in the interval between [1,m] and M.

The method for finding the original root A of modulus p:

because P is a prime number, so φ (p) =p-1, the problem is to find the smallest a so that a^ (p-1)%p = 1 established (according to the Fermat theorem, the formula must be established),

First of all, p-1 all the different quality factor p1,p2...pm,

For any integer a∈[1,p-1], verify whether a is the root of P,

Test method: a^ ((p-1)/p1), a^ ((p-1)/p2),... a^ ((p-1)/pm) There is a modulo p equal to 1,

If a is present, a is not a root of modulo p (that is, p-1 is not a-to-modulo p-order), otherwise a is the original root.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5#include <queue>6 #defineCLR (A, B) memset ((a), (b), sizeof ((a)))7 using namespacestd;8typedefLong Longll;9 Const intN =50000;Ten intPrime[n];//Prime[0] The number of primes that are stored One intPpri[n];//the quality factor of p-1 A voidGetprime () { -The CLR (Prime,0); -      for(inti =2; i < N; i++){ the         if(!Prime[i]) -prime[++prime[0] ] =i; -          for(intj =1; J <= prime[0] && Prime[j] <= n/i; J + +){ -prime[Prime[j] * I] =1; +             if(i% prime[j] = =0) Break; -         } +     } A } at ll Pow_m (ll A, ll N, ll m) { -ll t = a%m; -ll r =1; -      while(N >0){ -         if(N &1) -R = R * t%m; int = t%m; -N >>=1; to     } +     returnR; - } the intDivideintN) {//decomposition of the mass factor of composite n *     intCNT =0; $      for(inti =1; Prime[i] * Prime[i] <= N; ++i) {Panax Notoginseng         if(n% prime[i] = =0){ -PPRI[++CNT] =Prime[i]; the              while(n% prime[i] = =0){ +N/=Prime[i]; A             } the         } +     } -     if(N >1) ppri[++cnt] =N; $     returnCNT; $ } - intMain () { -     intp, I, A, T, F; the getprime (); -scanf"%d", &p);Wuyi     intCNT = Divide (P-1);//the number of qualitative factors in p-1 the      for(A =2; A <= P-1; ++a) {//original root from 2 to P-1 enumeration -f =1; Wu          for(i =1; I <= CNT; ++i) { -t = (P-1) /Ppri[i]; About             if(Pow_m (A, t, p) = =1){ $             //exists a^ ((p-1)/ppr[i]) mod p = 1, then A is not the original root of prime number p -f =0; Break; -             } -         } A         if(f) { +printf"%d\n", a); the              Break; -         } $     } the     return 0; the}
View Code

51nod 1135 Original Root

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.