洛谷 1082 同余方程——exgcd(水题)

题目:https://www.luogu.org/problemnew/show/P1082

大水题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a,b,x,y;
void exgcd(int a,int b,int &x,int &y)
{
    if(!b){x=1;y=0;return;}
    exgcd(b,a%b,y,x); y-=(a/b)*x;
}
int main()
{
    scanf("%d%d",&a,&b);
    exgcd(a,b,x,y);
    x=(x%b+b)%b;
    printf("%d\n",x);
    return 0;
}

 

posted on 2018-10-03 08:41  Narh  阅读(81)  评论(0编辑  收藏  举报

导航