您的位置:首页 > 其它

Romantic(裸扩展欧几里德)

2015-10-25 13:34 218 查看

Romantic

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3944 Accepted Submission(s): 1638


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

77 51
10 44
34 79

[align=left]Sample Output[/align]

2 -3
sorry
7 -3

题解:
等式已经说了,因为x无符号;所以x就只能是正数;下面是无符号的定义:
/*****************************************************/
计算机里的数是用二进制表示的,最左边的这一位一般用来表示这个
数是正数还是负数,这样的话这个数就是有符号整数。如果最左边这
一位不用来表示正负,而是和后面的连在一起表示整数,那么就不能
区分这个数是正还是负,就只能是正数,这就是无符号整数。
/****************************************************/
找到x,就得到y了;
代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
/*
计算机里的数是用二进制表示的,最左边的这一位一般用来表示这个
数是正数还是负数,这样的话这个数就是有符号整数。如果最左边这
一位不用来表示正负,而是和后面的连在一起表示整数,那么就不能
区分这个数是正还是负,就只能是正数,这就是无符号整数。
*/
void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;
x=1;
y=0;
}
else{
e_gcd(b,a%b,d,x,y);//写成a&b了。。。错了半天
LL temp=x;
x=y;
y=temp-a/b*y;
}
}
void cal(LL a,LL b,LL c){
LL x,y,d;
e_gcd(a,b,d,x,y);
//    printf("%lld %lld %lld \n",d,x,y);
if(c%d!=0){
puts("sorry");
return;
}
x*=c/d;//c,d都是1,可以省略;
//x=x0+b/d*t;
b/=d;//这个要记清。。。。。
if(b<0)b=-b;
LL ans=x%b;
if(ans<=0)ans+=b;//写成<=b了,又错半天。。。
printf("%lld %lld\n",ans,(1-ans*a)/b);
}
int main(){
LL a,b;
while(~scanf("%lld%lld",&a,&b)){
cal(a,b,1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: