同余方程
(mod.cpp/c/pas)
【问题描述】
求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解。
【输入】
输入文件为mod.in。
输入只有一行,包含两个正整数 a, b,用一个空格隔开。
【输出】
输出文件为mod.out。
输出只有一行,包含一个正整数 x0,即最小正整数解。输入数据保证一定有解。
【输入输出样例】
mod.in
3 10
mod.out
7
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
int e_gcd(int a,int b,int &x,int &y){
if(b==0){
x=1;y=0;
return a;
}
int ans=e_gcd(b,a%b,x,y);
int temp=x;
x=y;
y=temp-a/b*y;
return ans;
}
int main(){
int x,y,a,b;
freopen("mod.in","r",stdin);
freopen("mod.out","w",stdout);
scanf("%d%d",&a,&b);
e_gcd(a,b,x,y);
//printf("%d %d",x,y);
x=x%b;
if(x<0) x+=b;
printf("%d",x);
return 0;
}