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. css+div网页设计(三)--与多种技术的混合应用

    本篇博客将介绍css与多种技术的混合应用,javascript可以为我们的页面更加方便的交互,xml使数据存储跟方便,ajax的异步更新可以加快我们网页的载入速度. 1.css与javascript ...

  2. ubuntu server 系统,更换阿里云源(用户更新源)

    Ubuntu安装完毕后,默认使用的是官方的源,在国内访问速度很慢,这里介绍更换为阿里云的源方法. 步骤如下: 1.备份源配置文件 sudo cp /etc/apt/sources.list /etc/ ...

  3. XAML设计器卡死

    在生成工程时,存在这样一个记录: “未能找到一个或多个间接引用的程序集.分析不需要这些程序集.但是,如果没有这些程序集,分析结果可能不完整”. 表现形式既不是错误,可也不是警告.之所以关注到这个问题, ...

  4. BZOJ 3083: 遥远的国度 dfs序,树链剖分,倍增

    今天再做一天树的题目,明天要开始专攻图论了.做图论十几天之后再把字符串搞搞,区域赛前再把计几看看. 3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 128 ...

  5. 第一个过滤器Filter

    过滤器实现Filter接口javax.servlet.Filter package com.henau.example; import java.io.IOException; import java ...

  6. CSS之Document方法的使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Linux命令(2):ls命令

    1.作用:列出目录的内容: 2.格式:ls [选项] [文件] [选项]为指定要查看文件相关的内容,若未指定文件默认查看当前目录下的所有文件: 3.常见参数: 如图: 4.使用实例: [yournam ...

  8. js利用数组length属性清空和截短数组

    1.使用length清空数组: 代码如下 复制代码 <script>    var arr1 = ['aaa','bbbb','http://www.111cn.net'];    ale ...

  9. 大数阶乘(c语言)

    大数阶乘.代码比较简单. #include<stdio.h> #include<string.h> #define MAXN 25000 // 如果你的阶乘N比较大,建议大一点 ...

  10. javaScript之 变量、作用域和内存问题

    <javaScript高级程序设计>第四章  读书笔记 4.1  基本类型 和 引用类型 的值 1. 基本类型值 包括:Undefined.Null.Boolean.Number 和 St ...