Given a, b, x is obtained about the minimum equation ax ≡ 1 (mod b) of
Input to ensure a certain solvability
2 ≤ a,b ≤ 2*109
# Explanations
ax ≡ 1 (mod b)
ax+by=1
REPRESENTATION OF GENERAL SOLUTION:
x = x0 + b * k
y = y 0 - a * k
Therefore, x 0 % B x can take is the minimum
Finally, take the remainder being used skills: the number of re-mold after mold plus die once
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 ll a,b; 5 ll exgcd(ll a,ll b,ll &x,ll &y){ 6 if(b==0){ 7 x=1;y=0; 8 return a; 9 } 10 ll d =exgcd(b,a%b,y,x); 11 y-=a/b*x; 12 } 13 int main(){ 14 cin>>a>>b; 15 ll x,y; 16 exgcd(a,b,x,y); 17 cout<<(x%b+b)%b<<endl; 18 }