multiplication inverse element and its seeking method

Source: Internet
Author: User
Tags greatest common divisor

Before we begin, we introduce the 3 theorems:

1. Multiplication inverse (also called the reciprocal in Wikipedia, of course, after mod p, is actually the countdown is not it?) ):

If Ax≡1 (mod p), and GCD (a,p) =1 (A and P coprime), then a multiplication inverse of modulo p is called X.

2. Fermat theorem (defined from Wikipedia):

If a is an integer,p is a prime number, then a multiple of p, which can be expressed as

If a is not a multiple of P , this theorem can also be written

3.

Expand Euclid

(defined from Wikipedia):

Known integers a and B, the extended Euclidean algorithm can find integers x, y (one of which is probably a negative number) while the greatest common divisor of A and B is obtained, so that they satisfy the Bézout equation.

Well, after we understand the above theorem, we begin to analyze the multiplication inverse: ax≡1 (mod p) This equation is described in Chinese as a multiplied by a number x and the modulo p equals 1, i.e. a%p*x%p=res,res%p=1; it looks like a simple equation of the congruence theorem. So here's the problem.

Why can we use the Fermat theorem to find the inverse element?

By Fermat theorem ap-1≡1 both sides at the same time multiply ap-1 ap-2≡ap-1, both sides at the same time besides ap-1 ap-2/ap-1≡1, deformed a*ap-2≡1 (mod p), the answer is already obvious: if A,p coprime, because a*ap-2≡1 (mod p) and a*x ≡1 (mod p), the x=ap-2 (mod p) can be quickly obtained with a quick power.

Why is it possible to use the extended Euclidean to get the inverse?

We all know that modulo is the remainder, like 12%3=12-12/3=1,18%2=18-18/5=3. (/is in addition to the program operation)

So ax≡1 (mod p) is ax-yp=1. The form of Y is ax+py=1, for the convenience of understanding the following we write P as B is ax+by=1. It means that x is the modulo b multiplication inverse of a, and y is the modulo a multiplication inverse of B. Then you can use the extension Euclid to beg.

Knowing how to calculate the inverse, then what is the use of the multiplication inverse element?

If the result is too large generally will let you model a number, to ensure that the result is not very large, and this number is generally 1e9+7, and this number is a prime, plus minus multiplication and modulo operation of the sequential exchange will not affect the results, but the division does not work. Some topics require the result mod a large prime number, if there is division in the original result, such as divided by a, that can be multiplied by the inverse of a substitution. (except that a number equals the reciprocal of it, although the inverse is not entirely reciprocal, it can be understood, after all, the multiplication inverse is the inverse extension).

The extension Euclidean has to seek the inverse meta code:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;voidEXGCD (ll a,ll b,ll& d,ll& x,ll&y) {    if(!B) {d = A; x =1; y =0; } Else{EXGCD (b, a%b, D, y, x); y-= x* (A/b);    }}LL INV (ll A, ll p) {ll d, x, y;    EXGCD (A, p, D, X, y); returnD = =1? (x+p)%p:-1;}intMain () {ll a,p;  while(1) {scanf ("%lld%lld",&a,&p); printf ("%lld\n", INV (a,p)); }}

multiplication inverse element and its seeking method

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.