Modular Inverse ZOJ - 3609 (Extended Euclidean inverse element, Fermat's little theorem cannot be used)

Modular Inverse

ZOJ - 3609

The modular modular multiplicative inverse of an integer a modulo m is an integer x such that . This is equivalent to . a-1x (mod m)ax≡1 (mod m)


Input

There are multiple test cases. The first line of input is an integer T ≈ 2000 indicating the number of test cases.

Each test case contains two integers 0 < a ≤ 1000 and 0 < m ≤ 1000.

Output

For each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".

Sample Input
3
3 11
4 12
5 13
Sample Output
4
Not Exist
8

References


Title: Find the inverse element

Idea: When I saw the inverse element during the competition, the first thing that came to my mind was Fermat's little theorem, using the fast power q_pow(a, m-2) to find it, but because I was not familiar with the theorem, I judged that a number does not have an inverse In the element time, I thought that if a, m are mutually prime and m is a prime number, Fermat's little theorem, the modulus must be a prime number, but after thinking about it, there is also an Euler function, so that it does not have to be a prime number, find Euler Pull the function, as long as a and m are mutually prime, the inverse element can be found for any a, but this way of writing and submitting will time out. Although I know that there is an extended Euclidean that can find the inverse element, it feels similar at the time. Moreover, extended Euclid was not very good at writing, so he did not make it. Then I searched for the solution after the end, and found that all blogs were written with extended Euclid. I searched for the difference between the two methods. There are indeed some differences between the two methods, and the efficiency of extended Euclid is slightly smaller than that of Fermat. The theorem is higher:

Exact words

The extended Euclidean algorithm is more efficient, the constant is small, and the time complexity is O(ln n)

Fermat's little theorem algorithm complexity is O(log2 n) In several tests, the constant seems to be larger than the previous method

Probably this is the card data.

Finally, the minimum positive integer is required. If the answer is less than or equal to 0, + modulo is required. Do not add the modulo again. If the answer is 0, add modulo and modulo again. It's not the same.

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int e_gcd(int a,int b,int &x,int &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    int ans = e_gcd(b,a%b,y,x);
    y -= a / b * x;
    return ans;
}

int main(){
    int t;
    while(~scanf("%d",&t)){
        while(t--){
        int a,m;
        int x,y;
        scanf("%d%d",&a,&m);
        int gcd = e_gcd (a, m, x, y);
        if(gcd != 1){
            printf("Not Exist\n");
            continue;
        }
        int years = x;
        years = years % m;
        if(ans <= 0) ans = ans + m;//Don't modulo m here, in case the answer is 0, the modulo is still 0
        printf("%d\n",ans);
    }
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324622616&siteId=291194637