Methods for finding inverse elements of multiplication

Source: Internet
Author: User

(The text below may be incorrect. Thank you for your advice)
The definition of multiplication inverse element seems to be based on the group. It is easy to understand and can be said to be a promotion of the reciprocal concept. Note that the inverse element of mod p of A is a ^-1, then a ^-1 satisfies AA ^-1 limit 1 (mod P)

Addition, subtraction, multiplication, and modulo operations do not affect the result, but Division does not. Some questions require that the result mod be a large prime number. If there is division in the original result, for example, dividing by a, it can be multiplied by the inverse element of.

In the mod P operation, A has a multiplication inverse element if and only if a and P are mutually qualitative. The general question is a large prime number, so as long as a is not a multiple of P, it is to multiply the inverse element.

Three methods are available:
1. Extend Euclidean. AA ^-1 then 1 (mod P), which can be converted to AA ^-1 + py = 1, that is, the ax + by = gcd (a, B) that extends Euclidean's solutions ). The most common solution.

Int X, Y; int extgcd (int A, int B, Int & X, Int & Y) {If (B = 0) {x = 1; y = 0; return A;} int GCD = exgcd (B, A % B, x, y); int TMP = x; X = y; y = TMP-(A/B) * Y; return GCD;}/* solve AX + by = gcd (a, B), that is, ax limit 1 (mod B ). The Return Value of the function is the maximum common divisor of A and B, and X is the inverse element of. Note that a and B cannot be reversed. */

 

2. from the Fermat's small Theorem A ^ (p-1) 1_1 (mod p) (p is a prime number), a slight deformation is aa ^ (P-2) 1_1 (mod P ), is it found? A ^ (P-2) is the inverse element of A, which can be obtained using a quick power.


3. the Internet saw a very powerful recursion of O (n), and found the first n reverse elements. I don't know how it was launched, however, the correctness can be simply proved (the mod P must be a prime number ).

First, the reverse element of 1 is 1, so there is no doubt.
Assuming that the inverse element of the number of I has been obtained
I ^-1 = (P % I) ^-1 * (p-[p/I]) % P. [] Indicates that the end is rounded up.
(P % I) ^-1 is actually (p-[p/I] I) ^-1, then we multiply it by I,
II ^-1 = (p-[p/I] I) ^-1 * (I-1) P + P-[p/I] I) % P,
In fact, it is II ^-1 = K ^-1 * (I-1) P + k) % P = 0 + 1 = 1, so that the certificate =. =

// The font is really bad ..

int[] inv = new int[MAXN];inv[1] = 1;for (int i = 2; i<MAXN; i++)    inv[i] = inv[MOD%i]*(MOD-MOD/i)%MOD;

 

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.