HDU 2669 Romantic(扩展欧几里德)

来源:互联网 发布:mac命令行安装mysql 编辑:程序博客网 时间:2024/06/06 04:31

题意:Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1.

而且要满足X是通解中最小的。

注意X可以取0就可以了

//31MS 1808K 761 B G++#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;ll e_gcd(ll a,ll b,ll &x,ll&y){    ll ans;    if(b==0)    {        ans=a;        x=1,y=0;    }    else    {        ans=e_gcd(b,a%b,y,x);        y-=(a/b)*x;    }    return ans;}int main(){    ll a,b,x,y;    while(~scanf("%I64d%I64d",&a,&b))    {        ll gcd=e_gcd(a,b,x,y);        if(gcd!=1)            puts("sorry");        else        {            while(x<0)            {                x+=b;                y-=a;            }            printf("%I64d %I64d\n",x,y);        }    }    return 0;}


0 0
原创粉丝点击