zoukankan      html  css  js  c++  java
  • D

    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. 

    InputThe input contains multiple test cases. 
    Each case two nonnegative integer a,b (0<a, b<=2^31) 
    Outputoutput 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
    这个题目的坑就在 输出上,,x必须为正数,,y必须为负数,想想也很有道理 因为 X*a + Y*b = 1. a,b,都是大于1 的所以x和Y要有一个小于0,一个大于0,结果才会等于1
    //直接用拓展欧几里得公式求出x和y,再除以a,b的最大公约数q,如果依然是整数,则输出,否则,x+=b,y-=a,再判断
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    ll x,y;
    
    ll exgcd(ll a,ll b){
        if(b==0) {
            x=1;
            y=0;
            return a;
        }
        ll r=exgcd(b,a%b);
        ll t=y;
        y=x-(a/b)*y;
        x=t;
        return r;
    }
     
    int main(){
        ll a,b;
        while(scanf("%lld%lld",&a,&b)!=EOF)
        {
            ll r=exgcd(a,b);
            if(r!=1){
                puts("sorry");        
                continue ;
            } 
            ll x1=x;
            ll y1=y;
            x1=(x1+b)%b;
    //        while(x1<0){
    //            x+=b/r;
    //        }
    //        while(y1>0){
    //            y1-=a/r;
    //        }
            y1=(y1-a)%a;
    
            printf("%lld %lld
    ",x1,y1);
        } 
        return 0;
    }


  • 相关阅读:
    记录一段QQ关于 UNIGUI 的Session 时间设定
    uniGUI Cannot read property 'remove' of null
    基于kbmMW Configuration Framework 实现配置文件对象化
    每日日报42
    每日日报41
    每日日报40
    解决Ajax无法跳转到其他界面
    每日日报39
    每日日报38
    《软件项目成功之道》阅读笔记01
  • 原文地址:https://www.cnblogs.com/Accepting/p/11355337.html
Copyright © 2011-2022 走看看