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;
    }


  • 相关阅读:
    Codeforces 1037D【BFS】
    hdu 4725 The Shortest Path in Nya Graph 【拆点】+【最短路】
    LightOJ 1074
    POJ1062昂贵的聘礼(经典) 枚举区间 +【Dijkstra】
    POJ 3159 Candies 【差分约束+Dijkstra】
    POJ 3126 Prime Path【BFS】
    ZOJ 1940 Dungeon Master【三维BFS】
    POJ 3279 Fliptile (二进制枚举)
    HDU 2612 find a way 【双BFS】
    POJ 3414 Pot (输出路径)【BFS】
  • 原文地址:https://www.cnblogs.com/Accepting/p/11355337.html
Copyright © 2011-2022 走看看