HDU 2669 Romantic

Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You. 
................................Write in English class by yifenfei

Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
 

Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
 

Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead. 
 

Sample Input
77 51

10 44

34 79
 

Sample Output
2 -3

sorry

7 -3
 

Author
yifenfei
 

Source
HDU女生专场公开赛——谁说女子不如男

题意:扩欧,裸题。

怎么可能每个妹子都喜欢数学呢!像我就对数学并不感冒,只有科普级别的数学物理才有意思,真正需要严格证明推理 公式的层面...告辞

每个测试数据,输入两个非负整数a,b,找出满足X*a+Y*b=1的 (暗示a b的最大公约数是1,即gcd(a,b)=1)  非负整数X  整数 Y.

如果没有就sorry.如果X有多解,取最小值的那组解。

#include <cstdio>
using namespace std;
long long a,b,x,y;
long long gcd(long long m,long long n)
    {
         if(n==0)
        return m;
         return gcd(n,m%n);
    }
void exgcd(long long a,long long b,long long &x,long long &y)
{
    if(a%b==0)
    {
        x=0;y=1;return ;
    }
    exgcd(b,a%b,y,x);        
    y-=x*(a/b); 
}
//上面两个都是模板。。。
int main()
{
    while(scanf("%lld%lld",&a,&b)!=EOF)
    {
        x=gcd(a,b);
        if(x!=1)
            printf("sorry
");
        else
        {
            exgcd(a,b,x,y);
            while(x<=0)
            {
                x=x+b;
                y=y-a;
            }
/*

在刘汝佳的《算法竞赛入门经典》里,有一个这样的结论:设a,b,c为任意整数。若方程ax+by=c的一组整数解为(x0,y0),则它的任意整数解都可以写成(x0+kb',y0-ka'),其中a'=a/gcd(a,b),b'=b/gcd(a,b),k取任意整数。那么可以根据这个结论,如果x<0,可以把x一直加b',y一直减a',直到x>=0为止。。
---------------------
作者:hexianhao
来源:CSDN
原文:https://blog.csdn.net/hexianhao/article/details/50608564
版权声明:本文为博主原创文章,转载请附上博文链接!*/  //就是这么简单粗暴地粘贴


            printf("%lld %lld
",x,y);
        }
    } 
    return 0;
}
原文地址:https://www.cnblogs.com/greenaway07/p/10479064.html