[NOIP2012 TG D2T1]同余方程

题目大意:求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解。

题解:即求a在mod b意义下的逆元,这里用扩展欧几里得来解决

C++ Code:

#include<cstdio>
using namespace std;
int a,b,x,y;
int exgcd(int a,int b,int &x,int &y){
    if(b==0){x=1;y=0;return a;}
    int t=exgcd(b,a%b,y,x);y-=a/b*x;return t;
}
int main(){
    scanf("%d%d",&a,&b);
    exgcd(a,b,x,y);
    while(x<0)x+=b;
    x%=b;
    printf("%d
",x);
    return 0;
} 
原文地址:https://www.cnblogs.com/Memory-of-winter/p/7813294.html