Reverse element [number theory]

Source: Internet
Author: User

There is nothing made in the past two days ../. t_t

The first time I came into contact with the game against Yuan was the last question in Dalian in 11 years. Sorry, no. How can I break through the online competition Z?

For example, (8/2) % 5
We need to calculate a * B * C * D * E * f * g.../Z before the product part ll can not be stored, so we need to multiply the MOD side
When Division Z is finally processed, Division Z may not be exhausted.
For example, in the previous example, if 8/5 = 3 and 3 are not divided into 2, the value of 2% 5 is multiplied at % 5.
2% 5 reverse RMB = 2 ^ (5-2) = 8 This is a method for calculating reverse RMB. There is always another way to extend the Euclidean algorithm.
(3*8) % 5 = 4 = 4% 5

===

When calculating (a/B) % mod, we usually need to calculate the reverse element P of B % mod first (the condition that B has a reverse element is gcd (B, MoD) = 1, apparently, the prime number must have a reverse element), and then result C is obtained by (A * P) % Mod. Here, the reverse element P of B satisfies (B * P) % mod = 1. Here is a simple proof:
(A/B) % mod = C; (B * P) % mod = 1 ;== "(a/B) * (B * P) % mod = C; = "(A * P) % mod = C;


We can see from the above that the conclusion is correct. Of course, B must be a factor of. Next we need to know how to calculate the reverse element p based on B and Mod. Extended Euclidean algorithms, we should all know that A and B are known, and a group of solutions (X, Y) is obtained to make a * x + B * y = 1. The obtained X is the reverse element of a % B, and Y is the reverse element of B % A (think about why? Let's take a look at B or a on both sides of the equation ). Call extgcd (B, Mod, x, y), and X is the reverse element P of B % Mod.
Another method is to calculate the reverse element P of B % mod, that is, P = B ^ (mod-2) % mod, because B ^ (mod-1) % mod = 1 (MoD is a prime number.).

From: http://hi.baidu.com/zhanggmcn/item/ef4dadceb4fb993e449416e7

Example 3/2% 5 = 4; 2*3% 5 = 1 => (3/2) * (2*8) % 5 = 4 => (3*8) % 5 = 4

===

For Extended Euclidean:
First, Euclidean extension is mainly used to solve problems related to linear equations. Therefore, we start from a linear equation. Now let us assume that this linear equation is a * x + B * Y = m. If this linear equation has solutions, there must be gcd (a, B) | M, that is,, the maximum common divisor of B can divide M (M % gcd (a, B) = 0 ). The proof is simple, because a % gcd (a, B) = B % gcd (a, B) = 0, therefore, a * x + B * y must be able to divide gcd (A, B). If the linear equation is true, m can be used to replace a * x + B * Y, so as to get the above conclusion, we can use the above conclusion to determine whether a linear equation has a solution.
So when the linear equation A * x + B * Y = m is established, how can we solve X and Y?
1. Make a1 = A/gcd (a, B), b1 = B/gcd (a, B), M1 = m/gcd (A, B ). If we can first find the X1 and Y1 that meet the equation A * X1 + B * Y1 = gcd (a, B), then x = x1 * m1, y = Y1 * m1. Due to the Euclidean Algorithm gcd (a, B) = gcd (B, A % B), a * X1 + B * Y1 = gcd (a, B) = gcd (B, A % B) = B * X2 + (a % B) * Y2. Now we only need to perform some deformation to get the formula used in the Extended Euclidean algorithm. So that K = A/B (quotient), r = A % B (remainder), then a = K * B + R. Therefore, r = A-K * B is taken into the upper formula to obtain a * X1 + B * Y1 = B * X2 + (a-(A/B) * B) y2 = A * y2 + B * (x2-(A/B) * Y2)
=> X1 = Y2, Y1 = x2-(A/B) * Y2. With these two formulas, we can see the change of the corresponding parameters X and Y when we use Euclidean to calculate the maximum common approx. Now, let's look at the code for extending the Euclidean algorithm. In fact, extending the Euclidean algorithm is while finding the maximum common divisor of A and B, the values of a group of X1 and Y1 that meet the equation A * X1 + B * Y1 = gcd (A, B) are also obtained. The highlighted part of the code below is the code of the standard Euclidean algorithm.

__int64 exGcd(__int64 a,__int64 b,__int64 &x,__int64 &y){    if(b==0){        x=1;        y=0;        return a;    }    __int64 g=exGcd(b,a%b,x,y);    __int64 temp=x;    x=y;    y=temp-(a/b)*y;    return g;}

2. then, a group of solutions for X and Y are x1 * m1, Y1 * m1, but since there are infinite solutions to the equation, in practice, the minimum positive value of X or Y is usually solved. How can we solve X? Start with the equation. Now, X and Y meet a * x + B * Y = m, then a * (x + N * B) + B * (Y-N * A) = M is also true. We can obtain x + N * B (n = ..., -2 ,...) It is the set of all the X solutions of the equation. Since each X must have a y corresponding to it, the value of Y can be ignored when solving X. Take K to make X + K * B> 0. The minimum positive value of X should be (x + K * B) % B. But is this value actually the smallest ?? If we divide the two sides of the equation by gcd (a, B) at the same time, the equation becomes A1 * x + B1 * Y = m1. The above analysis shows that, at this time, the minimum value should be (x + K * B1) % B1. Because B1 <= B, this value must be less than or equal to the previous value. In the actual solution process, while (x <0) x + = B1 is usually used to satisfy the positive conditions. In order to exit the loop faster, you can change B1 to B (B is a multiple of B1), multiply B by a multiple, and then add it to X.

From above: http://cie.xtu.edu.cn/acmfan_cn/viewthread.php? Tid = 844

===

Extended Euclidean reverse meta template: # include <iostream> # DEFINE _ int64 long longusing namespace STD; // For example, 3x + 4y = 1 AX + by = 1 // obtain a group of solutions X0 =-1, Y0 = 1 and x =-1 + 4 K, y = 1-3kinline _ int64 extend_gcd (_ int64 A ,__ int64 B ,__ int64 & X ,__ int64 & Y) // AX + by = 1 returns, in GCD of B, the minimum positive integer solution {_ int64 ans, T; If (B = 0) {x = 1; y = 0; return A;} ans = extend_gcd (B, A % B, x, y); t = x; X = y; y = T-(A/B) * Y; return ans;} // (a/B) % mod = C reverse element is P, (p * B) % mod = 1 // (A/B) * (p * B) % mod = C * 1% mod = C // (P * B) % mod = 1 is equivalent to p * B-(P * B)/MoD * mod = 1 where P, B is known to be equivalent to ax + by = 1 // where x = p (x is the reverse element), Y = P/MoD, A = B, B = B * mod then call extend_gcd (B, B * mod, x, y) to calculate (a/B) % mod inverse element is equivalent to a * P % modint main () {_ int64 a, B, X, Y, C, gcd, Mod, P; // AX + by = C while (CIN> A> B> C) {GCD = extend_gcd (a, B, x, y); cout <x <"" <Y <Endl; If (C % gcd) {cout <"no solution! "<Endl; continue;} cout <" x = "<x * C/GCD <" Y = "<y * C/GCD <Endl ;} return 0 ;}

From: http://hi.baidu.com/foreverlin1204/item/5e3da7d437b4d290270ae76c

===

According to the Dalian I question and the great god code, another template was changed:

Paste template:

#include <iostream>using namespace std;int x,y,rev;
Void extend_euclid (int A, int B) {If (B = 0) {x = 1; y = 0; return;} extend_euclid (B, A % B ); int T = x; X = y; y = T-A/B * Y;} int main () {// B % mod, int B, MOD; while (CIN> B> mod) {// x = 0; y = 0; extend_euclid (B, MoD); cout <(X % mod + mod) % mod <Endl;} return 0 ;}

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.