## Euclid:

First, we must clarify the Euclidean algorithm, which is what we call the tossing and dividing method.

Is to find the greatest common factor gcd(a,b) of two numbers a, b

```
ll gcd(ll a,ll b) //欧几里得算法
{
//辗转相除法
if(a%b==0) return b;
return gcd(b,a%b);
}
```

## Extended Euclidean algorithm

Generally speaking, it is to find all the solutions of (x, y) of the binary linear equation Ax+By=gcd(A, B). (Assuming A and B are known)

Our method is to obtain his general solution through a special solution of this set of equations.

Then there are two questions:

**1. How to get his special solution? **

**2. How to get a general solution from the special solution? ******

**1. Find special solutions**

From Euclid's algorithm, gcd(A, B) can be written as gcd(B, A%B).

Get a new equation

Bx2+(A%B) * y2=gcd(B，A%B);-------------1

A%B=A-(A/B)*B;

rewrite the equation again

Bx2+[A-(A/B) * B ]y2=gcd(B，A%B);*------------2

When gcd (B, A% B) has been written down until A% B = 0;

by Equation No. 1 obtained

Bx2 = B;

**this time we have come to a group of specialized solutions to equation x2 = 1, y2 = 0 ;**

At this time we have solved the first problem to find a special solution for the system of equations.

**2. How to get the general solution from the special solution**

Continue to observe Equation 2. Now that we have obtained special solutions x2 and y2. Then if we know the relationship between x2 and x, and the relationship between y2 and y, we can get the general solution (x, y).

Now organize the formula 2 to get:

Ay2+B * [x2-(A/B)*y2]=gcd

The original formula is Ax+By=gcd.

At this time, we can get the relationship between the general solution and the special solution.

x = y2;

y = x2- (A / B) * y2

Because our process of seeking special solutions is recurring step by step, our process of seeking general solutions is a retrospective process.

Let’s take an example of 3x+5y=1

Here (2, -1) is a set of solutions of the equation 3x+5y=1

** **Summary: In the process of finding the general solution, we mainly use the relationship between the general solution and the special solution we obtained above, because the coefficients of A and B in each step are Different, so we have to go back from the special solution to the general solution step by step**

Expand Euclid

#include

#include

#include

#include

*Let us consider another question: how to get multiple solutions from a set of general solutions?*

It must be that the value of x, y increases and decreases to ensure that the equation

ax+by=gcd is established.

We know that the least common multiple of a, b is equal to (a*b)/gcd(a,b),

then when x=x+ b/gcd(a,b), when y=ya/gcd(a,b), substitute the original formula to

get a *(x+b/gcd(a,b)) + b *( ya/gcd(a,b) ))

It is not difficult to see that every time the value of x or y is increased or decreased by a least common multiple of a, b, the value of the original formula remains unchanged.

Then we can get the minimum period of change of x or y to be b/gcd(a,b) or a/gcd(a,b).

In addition, it is not difficult to figure out if Ax+By=C, if C%gcd(A, B)!=0, then the system of equations must have no solution

From this we derive the smallest positive integer solution

**

## Smallest positive integer solution

**

Extended Euclid is mainly used to solve the solution of equation

ax≡b(mod)l

ax-ly=b;

from the above extended Euclid algorithm, we can find x, but x may be negative, or x may not be It is not the smallest positive integer solution.

We have the period of x. In this formula, the period T=l/gcd(a,l);

we can choose such a processing method

x=((x%T)+T) %T;

ensure that x is the smallest positive integer solution

## Multiplicative inverse

All integers in a mold system comprising only p [1, p-1], so the calculation of the mold based on the division of time since p is not divisible accuracy problems occur, so we introduce multiplicative inverse

ax≡1 (mod) p

which When x is the inverse element of a in the modular system p.

After learning to expand Euclidean,

transform the existence condition of the inverse element x of

ax-pk=1

a to gcd(a,p)=1.

Also use the extended Euclidean The obtained code can find the inverse element x of a;

```
**拓展欧几里得，乘法逆元，最小整数解**
#include <iostream>
#include <cstring>
#include <algorithm>
#include<map>
using namespace std;
const int N=1e6+10;
typedef long long ll;
typedef unsigned long long ull;
l
ll exgcd(ll a,ll b,ll &x,ll &y) {
if(b==0) {
x=1;
y=0;
return a;
}
ll res= exgcd(b,a%b,x,y);
ll t;
t=x;
x=y;
y=t-a/b*y;
return res;
}
int main() {
// ax-pk=1
//p =1000000007
ll a,b,x,y;
cin>>a>>b;
exgcd(a,b,x,y);
ll ans=exgcd(a,b,x,y);
// ax+by=c
x=x*c/gcd(a,b);
ll t=b/ans;
x=(x%t+t)%t;
cout<<x;
return 0;
}
```

Welcome to correct