• 扩展欧几里得--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;
    }
    
    
  • 相关阅读:
    ECharts
    JavaScript实现数组去重方法
    面试题1
    Vue中的8种组件通信方式
    渐进式web应用 (PWA)
    单元测试(Jest 和 Mocha)
    axios
    Vue Router
    Vuex
    hash 模式与 history 模式小记
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/11681228.html
Copyright © 2020-2023  润新知