![[NOIP2017提高组]小凯的疑惑-扩展欧几里得 [NOIP2017提高组]小凯的疑惑-扩展欧几里得](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
#include<bits/stdc++.h>
using namespace std;
long long a,b,x,y,ans,tmp;
inline void ex_gcd(long long a,long long b,long long &x,long long &y){
if(!b){
x = 1;
y = 0;
return;
}
ex_gcd(b,a%b,y,x);
y -= (a/b)*x;
}
int main(){
cin>>a>>b;
ex_gcd(a,b,x,y);
if(x > 0)swap(a,b),swap(x,y);
tmp = (-x)/b;
x = x+tmp*b;
y = y-tmp*a;
while(x < 0)x += b,y -= a;
while(x > 0)x -= b,y += a;
tmp = x+b;
ans = a*(tmp-1)+b*(y-1);
cout<<ans-1<<endl;
return 0;
}
还有特别巨小伙伴直接用
#include<bits/stdc++.h>
using namespace std;
long long a,b,ans,sum,t;
int main(){
scanf("%lld %lld",&a,&b);
if (a>b)swap(a,b);
t=(a-1)*b/a-1;
sum=(sum+(a-1)*b)%a;
printf("%lld",t*a+sum);
return 0;
}