Romantic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3944 Accepted Submission(s): 1638
Problem 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
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)
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.
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
10 44
34 79
Sample Output
2 -3
sorry
7 -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=;
y=;
}
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!=){
puts("sorry");
return;
}
x*=c/d;//c,d都是1,可以省略;
//x=x0+b/d*t;
b/=d;//这个要记清。。。。。
if(b<)b=-b;
LL ans=x%b;
if(ans<=)ans+=b;//写成<=b了,又错半天。。。
printf("%lld %lld\n",ans,(-ans*a)/b);
}
int main(){
LL a,b;
while(~scanf("%lld%lld",&a,&b)){
cal(a,b,);
}
return ;
}