扩展欧几里得exgcd
1.https://www.luogu.org/problemnew/solution/P1082
2.求 \(ax + by = gcd(a,b)\)中的未知数
3.\(由ax\equiv1(mod b)\)可得ax+by=1
a,b为gcd(a,b)倍数,so \(ax+by=z(if为1)\)要有解,z(if为1)必须为gcd(a,b)(那 么为一)的倍数
4.再根据gcd反推
5.code
x的返回值必须为1=》gcd(a,b)x+0*y=gcd(a,b);
y的返回值无要求,但要小心溢出
\(ax+by=bx_2+(a mod b)y_2\)
\(ax+by=ay_2+b(x_2−(a/b)y_2)\)
#include<bits/stdc++.h>
#define re return
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define ll long long
char buf[1<<21],*p1,*p2;
inline int getc(){re p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
template<typename T>inline void rd(T&x)
{
char c;bool f=0;
while((c=getc())<'0'||c>'9')if(c=='-')f=1;
x=c^48;
while((c=getc())>='0'&&c<='9')x=x*10+(c^48);
if(f)x=-x;
}
using namespace std;
ll a,b,c,x,y;
ll exgcd(ll a,ll b)
{
if(b==0){x=1;y=0;re a;}
ll ans=exgcd(b,a%b);
ll k=x;
x=y;
y=k-a/b*y;
re ans;
}
int main()
{
// freopen("in.txt","r",stdin);
rd(a),rd(b);
int z=exgcd(a,b);
printf("%lld",(x%b+b)%b);
re 0;
}