HDU 2669 Romantic(扩展欧几里德)

题意: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;
}


你可能感兴趣的:(HDU 2669 Romantic(扩展欧几里德))