zoukankan      html  css  js  c++  java
  • 扩展欧几里得--hdu2669

    hdu-2669

    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

    img

    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 Xa + Yb = 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
    

    题意

    若存在满足ax+by=1的x,y,则输出非负整数x,整数y,否则输出sorry

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <iomanip>
    #include <cstdio>
    
    using namespace std;
    typedef long long LL;
    typedef pair<double, double> PDD;
    typedef pair<LL, LL> PLL;
    
    const LL N = 1e6+50;
    const LL MOD = 1e9+7;
    const LL INF = 0x3f3f3f3f;
    
    #define lson l, m, rt>>1
    #define rson m+1, r, rt>>1|1
    
    LL exgcd(LL a, LL b, LL &x, LL &y)
    {
        if(!b)
        {
            x = 1;y = 0;
            return a;
        }
        LL ret = exgcd(b, a%b, y, x);
        y -= x*(a/b);
        return ret;
    }
    
    LL gcd(LL a, LL b)
    {
        return b?gcd(b, a%b):a;
    }
    
    int main()
    {
        LL a, b, x, y;
        while(cin >> a >> b)
        {
            if(gcd(a, b) != 1) puts("sorry");
            else
            {
                exgcd(a, b, x, y);
                while(x < 0)
                {
                    x += b;y -= a;
                }
                cout << x << " " << y << endl;
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    jQuery attr 与 prop 区别最简单分析
    Js事件处理模型/周期
    canvas实现点击带水纹的按钮
    js作用域问题
    js 函数里的 this
    css3: scrollLeft,scrollWidth,clientWidth,offsetWidth 的区别
    C# 中的Async 和 Await 的用法详解
    1、Task的优势
    探秘C#中的yield关键字
    详解C#中 Thread,Task,Async/Await,IAsyncResult的那些事儿
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/11681228.html
Copyright © 2011-2022 走看看