Description

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. 
 

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
 
 
题意:给你a和b,叫你求ax+by=1的x和y,要求是x为正整数,y为整数...   有就输出,没有就输出sorry。
 
 
题解:这里用到了扩展欧几里德算法。先用扩展欧几里德算法求出x,y然后再判断x是否大于0,如果小于0,则通过循环+b,直到x>0,在输出答案
 
这里给出两种代码,一种是书上的扩展欧几里德程序,一种是网上的(他们说是模板).....
 
 
 
代码如下:(最后打注释的主函数是开始自己打的,但是没有考虑x小于0的时候,所以wa了)
 
 
 #include <stdio.h>
void gcd(int a,int b,int &d,int &x,int &y)
{
if(!b)
{
d=a;
x=;
y=;
}
else
{
gcd(b,a%b,d,y,x);
//printf("a=%d b=%d d=%d y=%d x=%d\n",a,b,d,y,x);
y-=x*(a/b);
//printf("y=-%d*(%d/%d) %d \n",x,a,b,y);
}
} int main()
{
int a,b,d,x,y;
while(scanf("%d%d",&a,&b)==)
{
gcd(a,b,d,x,y);
if(d==)  //d=gcd(a,b),d为a,b的最大公约数。
{
if(x>)
printf("%d %d\n",x,y);
else
{
while(x<=) //求另外的解  例如:5x+6y=1  第一种解:x=-1,y=1  第二种 x=5 y=-4
{  // 这里通过x=x+b和y=y-a来算。  就等于  (x+b)*a+(y-a)*b  最终算式结果还是不变的
x+=b;
y-=a;
}
printf("%d %d\n",x,y);
}
}
else
printf("sorry\n");
}
return ;
}
/*int main()
{
int a,b,d,x,y;
while(scanf("%d%d",&a,&b)==2)
{
gcd(a,b,d,x,y);
//printf("%d %d %d\n",x,y,d);
if(x%d==0&&y%d==0&&x/d>0)
printf("%d %d\n",x/d,y/d);
else
printf("sorry\n");
}
return 0;
}*/

网上的:

 #include<cstdio>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
int r=exgcd(b,a%b,x,y);
//printf("a=%d b=%d\n",a,b);
int t=x;
//printf("t=%d\n",x);
x=y;
//printf("x=%d\n",y);
y=t-a/b*y;
//printf("y=%d\n",y);
return r;
}
int main()
{
int a,b,x,y,m;
while(scanf("%d%d",&a,&b)!=EOF)
{ m=exgcd(a,b,x,y);
if(m==)
{
while(x<)
{
x+=b;
y-=a;
}
printf("%d %d\n",x,y);
} else
printf("sorry\n");
}
return ;
}
 

HDU 2669 第六周 I题的更多相关文章

  1. hdu 4548 第六周H题(美素数)

    第六周H题 - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   De ...

  2. HDU 1465 第六周L题

    Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!  做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样.  ...

  3. HDU 1405 第六周 J题

    Description Tomorrow is contest day, Are you all ready?  We have been training for 45 days, and all ...

  4. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  5. Codeforces 559A 第六周 O题

    Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...

  6. HDU1465 第六周L题(错排组合数)

    L - 计数,排列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descrip ...

  7. 第六周 E题 期望.....

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  8. 第六周 N题

    Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...

  9. 第六周O题(等边三角形个数)

    O - 计数 Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Descripti ...

随机推荐

  1. android studio还不错

    今天体验了哈 Android Studio,还不错同Elipse类似

  2. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  3. 控制语句(if-else+循环+switch)汇编规则

    [1]说说条件码 最常用的的条件码有: CF:进位标志 (无符号溢出) ZF:零标志 SF:符号标志(结果为负数) OF:溢出标志 (补码溢出, 有符号溢出) [2]有两类指令设置条件码而不改变任何其 ...

  4. iOS10.1不能调试解决方案

    对于一些有强迫证的程序员来说只要苹果更新系统,会马上更新,但是军哥更新过10.1的系统后发现Xcode8不能调试了 苹果这次太不人性话了,解决办法,最简单的是 拷贝iOS10.1支持文件放在Xcode ...

  5. Java计算机网络

    计算机网络: 分布在不同的地域的计算机通过外接设备(路由器),实现共享和数据传输网络编程:网络编程主要是用来解决计算机和计算机之间的数据传输网络编程:不需要基于HTML就可以达到数据之间的传输,比如Q ...

  6. 使用Jeditable插件时遇到的问题

    Jeditable在渲染页面已有DIV=>form的时候 首先使用 $("div").html(); 去获取原DIV中的内容. 这样导致一个问题, 如果原div中带有html ...

  7. JavaScript--模块化编程(笔记)

    一直对JS都是一知半解,最近遇到这方面问题,所以在网上学习了一下,现在还没有完全明白,先贴出笔记; 第一章 JavaScript模块化编程(一):模块的写法 一 原始写法 // 模块就是实现特定功能的 ...

  8. ASP.NET缓存全解析4:应用程序数据缓存 转自网络原文作者李天平

    System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和数据集.它还为这些对象提供了失效 ...

  9. 20150303--从SQL中获取数据的三级联动

    省市地区的三级联动,每变更一次所选地都需要提交,但是又不需要把整个页面提交,所以我们需要使用控件:UdataPanel.工具--AJAX扩展 还有ScriptManager,并要将其放在页面的最顶端. ...

  10. PHP的接口(interface)

    接口声明了函数和字段,但不会给出实现的细节 规则: 1.类全部为抽象方法(不需要声明abstract) 2.接口抽象方法必须是public 3.成员(字段)必须是常量 interface Comput ...