HDU 2669 第六周 I题

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了)

 1 #include <stdio.h>
 2 void gcd(int a,int b,int &d,int &x,int &y)
 3 {
 4     if(!b)
 5     {
 6         d=a;
 7         x=1;
 8         y=0;
 9     }
10     else
11     {
12         gcd(b,a%b,d,y,x);
13         //printf("a=%d  b=%d  d=%d  y=%d  x=%d\n",a,b,d,y,x);
14         y-=x*(a/b);
15         //printf("y=-%d*(%d/%d)  %d \n",x,a,b,y);
16     }
17 }
18
19 int main()
20 {
21     int a,b,d,x,y;
22     while(scanf("%d%d",&a,&b)==2)
23     {
24         gcd(a,b,d,x,y);
25         if(d==1)         //d=gcd(a,b),d为a,b的最大公约数。
26         {
27             if(x>0)
28                 printf("%d %d\n",x,y);
29             else
30             {
31                 while(x<=0)    //求另外的解   例如:5x+6y=1  第一种解:x=-1,y=1  第二种 x=5  y=-4
32                 {              //                 这里通过x=x+b和y=y-a来算。   就等于    (x+b)*a+(y-a)*b    最终算式结果还是不变的
33                     x+=b;
34                     y-=a;
35                 }
36                 printf("%d %d\n",x,y);
37             }
38         }
39         else
40             printf("sorry\n");
41     }
42     return 0;
43 }
44 /*int main()
45 {
46     int a,b,d,x,y;
47     while(scanf("%d%d",&a,&b)==2)
48     {
49         gcd(a,b,d,x,y);
50         //printf("%d %d  %d\n",x,y,d);
51         if(x%d==0&&y%d==0&&x/d>0)
52             printf("%d %d\n",x/d,y/d);
53         else
54             printf("sorry\n");
55     }
56     return 0;
57 }*/

网上的:

 1 #include<cstdio>
 2 using namespace std;
 3 int exgcd(int a,int b,int &x,int &y)
 4 {
 5     if(b==0)
 6     {
 7         x=1;
 8         y=0;
 9         return a;
10     }
11     int  r=exgcd(b,a%b,x,y);
12     //printf("a=%d b=%d\n",a,b);
13     int t=x;
14     //printf("t=%d\n",x);
15     x=y;
16     //printf("x=%d\n",y);
17     y=t-a/b*y;
18     //printf("y=%d\n",y);
19     return r;
20 }
21 int main()
22 {
23     int a,b,x,y,m;
24     while(scanf("%d%d",&a,&b)!=EOF)
25     {
26
27         m=exgcd(a,b,x,y);
28         if(m==1)
29         {
30             while(x<0)
31             {
32                 x+=b;
33                 y-=a;
34             }
35             printf("%d %d\n",x,y);
36         }
37
38         else
39             printf("sorry\n");
40     }
41     return 0;
42 }
时间: 2024-11-10 13:39:30

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

HDU 1465 第六周L题

Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!  做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样.  话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的.比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情.如果套用一句经典的评语,我们可以这样总结:一个人做错一道选择题

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

第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如"it's"算一个单词,长度为4.注意,行中可能出现连续的空格. 输入格式: 输入在一行中给出一行文本,以'.'结束,结尾的句号不能计算在最后一个单词的长度内. 输出格式: 在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后

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

L - 计数,排列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!  做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样.  话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的.比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然

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

O - 计数 Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to . Then he measured the length of it

第六周 N题

Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermi

集训第六周 E题

E - 期望(经典问题) Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once.

第六周 G题

G - 数论,最大公约数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must hide in one of the holes. A wolf searches the rabbit in antic

Light OJ 1104 第六周F题

F - 概率(经典问题) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there ar

HDU 4923 Room and Moor (多校第六场C题) 单调栈

Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that: Input The inp