拓展欧几里得
适合题型:
。。。一些莫名其妙要用到GCD啊,LCM的题
样题:同余方程
求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解。
样例数据
输入
3 10
输出
7
备注
【数据范围】
对于 40% 的数据,2≤b≤1,000;
对于 60% 的数据,2≤b≤50,000,000;
对于 100% 的数据,2≤a,b≤2,000,000,000。
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int X=0,w=1; char ch=0;
while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
return X*w;
}
ll a,b,x,y;
void gcd(ll a,ll b,ll&x,ll&y){(!b)?(x=1,y=0):(gcd(b,a%b,y,x),y-=a/b*x);}
int main(){cin>>a>>b;gcd(a,b,x,y);cout<<(x+b)%b<<endl;}