Simple Number Theory

Source: Internet
Author: User
Time Limit: 3000/1000 ms (Java/other) memory limit: 65535/32768 K (Java/other) total submission (s): 15 accepted submission (s): 5 Font: times New Roman | verdana | georgiafont size: Drawing → problem descriptiongiven two positive integers A and N, satisfaing gcd (A, n) = 1, please find the smallest positive integer x with a ^ x limit 1 (mod N ). inputfirst is an integer T, indicating the number of test cases. T <3001.
Each of following T lines contains two positive integer a and N, separated by a space. A <n <= 1000000. outputfor each test case print one line containing the value of X. sample input
22 33 5
Sample output
24
Authorhynu // question: Find the minimum x value that satisfies a ^ x mod n constant = 1. // Background: For any two positive integers a, m, m> = 2, there is a ^ PHI (m) 1_1 (mod m) PHI (m) that is, the Euler's function of M, that is, the Euler's theorem. When M is a prime number P, this formula is: a ^ (p-1) 1_1 (mod m) it indicates that if M is a prime number, then the Euler's function of M is expressed as m-1. That is, ferma's theorem. // Question: First name the values of the 1-N Euler's function and then enumerate the Euler's function for prime factor decomposition to continuously update the minimum value.
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> using namespace STD; const int max = 1000010; int prime [Max], phi [Max]; // Euler's function for saving all values void fun () // evaluate the Euler's function for all values from 1 to max {Prime [0] = prime [1] = 0; For (INT I = 2; I <= max; I ++) prime [I] = 1; for (INT I = 2; I * I <= max; I ++) if (prime [I]) for (Int J = I * I; j <= max; j + = I) prime [J] = 0; For (INT I = 1; I <= max; I ++) Phi [I] = I; for (INT I = 2; I <= max; I ++) if (prime [I]) f Or (Int J = I; j <= max; j + = I) Phi [J] = Phi [J]/I * (I-1);} int Mod (int, int B, int c) // fast idempotent modulo {int ans = 1; long AA = A; while (B) {If (B % 2) ans = ans * Aa % C; AA = AA * Aa % C; B/= 2;} return ans;} int main () {// freopen ("a.txt ", "r", stdin); // freopen ("B .txt", "W", stdout); fun (); int T, A, N; CIN> T; while (t --) {CIN> A> N; int Sn = (INT) SQRT (PHI [N]), ANS = N; // enumerate the Euler's factor of N between 1-sn For (INT I = 1; I <= Sn; I ++) // find all the factors in the Euler's function that meet the conditions and are the smallest. {If (PHI [N] % I = 0) // If I is a factor of Phi, update the minimum value {If (mod (A, I, n) = 1 & Ans> I) ans = I; If (mod (A, Phi [N]/I, n) = 1 & Ans> Phi [N]/I) ans = Phi [N]/I ;}} cout <ans <Endl ;}return 0 ;}

 

Simple Number Theory

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.