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


  • 相关阅读:
    找出数组中重复的值
    算法-二分法查询
    MySQL连接数据库url的参数characterEncoding=UTF-8
    String 与 list 相互转换
    php配置debug
    ideal+php
    命令行编译tomcat项目
    jsp+layui导出excel
    jsp+ssm+tomcat+ueditor上传定时处理无用文件
    jsp后台获取项目路劲
  • 原文地址:https://www.cnblogs.com/Accepting/p/11355337.html
Copyright © 2011-2022 走看看