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;
    }
    
    
  • 相关阅读:
    使用doctest单元测试方式培训讲解:Python字符串格式化(适合测试工程师的学习方法)
    树莓派Raspberry中成功安装RobotFramework+Selenium
    树莓派Raspberry实践笔记-安装使用minicom
    树莓派Raspberry实践笔记-解决官方文档打开慢问题
    window电脑蓝屏
    fastjson基本用法
    自定义注解
    curl工具
    观察者模式
    工厂模式
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/11681228.html
Copyright © 2011-2022 走看看