Extended Euclid self-study notes

On the board of the Los Valley https://www.luogu.org/problem/P1082
this question to analyze:
mathematical conversion, derivation
is seeking ax≡1 (modb)
change if: ax% b = 1 --- " ax + by = 1
course y just a "Lei Feng" unknown, whether it be positive or negative it (but y is an integer) final demand is x
to ax + by = 1, we exgcd to solve. exgcd the original solution to solve ax + by = gcd (x, y) of.
Here there must be solutions, then a, b coprime, gcd (a, b) = 1
demonstrate: gcd (a, b) and b is a factor, it is ax + by GCD (a, b) multiple (x , y is an integer), the title for this equation: ax + by = 1, then the GCD (a, b) can only be 1, so a, b coprime
canPleasant mannerWith the exgcd! (Unfortunately, many expression ah also pushed)
by the already obtained: gcd (a, b) = 1 ( above has been demonstrated) gcd (a, b) = gcd (b, a% b) ( Common GCD) to do this problem it
is known original formula: ax + by = gcd (a , b) assume xx, yy is gcd (b, a% b) answer
is b xx + (a% b) YY = gcd (b, a % b), and we have to know gcd (a, b) = gcd (b, a% b), and gcd (a, b) = 1
then A X + B Y =. 1 B XX + (A% B) YY = A 1 the X-+ b the y-b = xx + (A% b) YY
is not feeling a little awkward%? In fact, it hinders glance answers processing: a% b = a- (int (a / b)) (int here represents rounding, oier habits, do not write int is also OK, but to reduce it to write here misunderstanding)
equation will become: A X + B Y = B XX + (A-int (A / B) B) YY
push: A
X + B Y = B XX + (A-int (A / B) B) A YY X B + Y B = XX + A YY B- int (A / B) YY A+ B X Y A = YY + B (XX-YY int (A / B))
can now be seen that a set of answers it, A
X A = YY B Y = (XX-YY int (A / B))
which title worth a good push to see if I draft it (of course, after I taught myself to do their own draft),
ab
gcd (a, b) = 1
AX + by = 1
AX + by = gcd (b, b a% )
B
XX + (A% B) YY = GCD (B, A% B)
B
XX + (A% B) YY = GCD (A, B)
B
XX + (A% B) YY =. 1
A
X + B Y = B XX + (A% B) YY
A
X + B Y = B XX + (ab & int (A / B)) YY
A X + B Y = B XX + A YY-B (int (A / B)) YY
A X + bA = the y- YY + b (xx-int (A / b) YY)
the X-the y-YY = = (xx-int (A / b)
YY)
is not very full because some of the evidence I have seen the name, no longer permit
program recursive
recursive implementation should be very clear it, as boundary conditions do still have the equation to start
ax + by = gcd (a, b) when b = 0 is gcd (a, b) obviously is gcd (a, 0) = a , then x is equal to 1 equation certainly established
I believe it can be seen that here y can be any value, of course, in theory, would have a direct let y = 0, otherwise may appear too large to cross-border value
as for the final answer : the minimum requirement is a positive integer, requires some processing, as obtained in just one solution
is further assumed that a Lei unknown C (integer)
is substituted into the equation: ax + by + abc-abc = 1 a (x + bc) + b (y-ac) = obviously 1 x, y becomes (x + bc) and (y-ac), and x, y is an integer of still
thus, to obtain any solution of this equation (with the above is denoted x) by subtracting or adding the multiple b the multiple b are solutions of this equation
that if x is negative, continue to add b, if the number is positive, a modulo b is the smallest positive integer solutions to ensure that
the following is the code of

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map> 
using namespace std;
#define re register int
#define int long long
inline int read(){
    int x=0,ff=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')ff=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return x*ff;
}
//萌新的推导过程,乱的一批
/*
a b
gcd(a,b)=1
ax+by=1
ax+by=gcd(b,a%b)
b*xx+(a%b)*yy=gcd(b,a%b)
b*xx+(a%b)*yy=gcd(a,b)
b*xx+(a%b)*yy=1
a*x+b*y=b*xx+(a%b)*yy
a*x+b*y=b*xx+(a-b*int(a/b))*yy
a*x+b*y=b*xx+a*yy-b*(int(a/b))*yy
a*x+b*y=a*yy+b*(xx-int(a/b)*yy)
x=yy y=(xx-int(a/b)*yy)
*/
int x,y,xx,yy;
void gcd(int a,int b){
    if(b==0){x=1;y=0;return;}
    gcd(b,a%b);
    xx=x;yy=y;
    x=yy;y=(xx-(a/b)*yy);
}
signed main(){
    int a,b;a=read();b=read();
    gcd(a,b);
    while(x<0)x+=b;
    x%=b;
    printf("%lld\n",x);
    return 0;
}

Well, for this problem is over

Guess you like

Origin www.cnblogs.com/ffrxy01bt/p/11261296.html