给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。
Input
输入2个数M, N中间用空格分隔(1 <= M < N <= 10^9)
Output
输出一个数K,满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。
Sample Input
2 3
Sample Output
2
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define long long LL;
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
void exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;
y=0;
return ;
}
exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-(a/b)*y;
}
int main()
{
int n,m,x,y;
cin>>n>>m;
exgcd(n,m,x,y);
printf("%d\n",(x+m)%m);
return 0;
}