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; }
相关文章推荐
- java值传递问题
- Google OKR 目标管理体系学习
- 多线程基础小问题
- 使用 Android NDK 的交叉编译工具链移植 C/C++ 项目到安卓平台
- wget jdk 却是网页的解决办法
- 创建一个已经存在数据的MySQL复制
- 迷茫的时候看一下
- UI控件
- java基础面试资料
- 使用opengl绘制五角星图形
- java-用两个栈实现队列&&用两个队列实现一个栈
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
- 转:iOS-生成Bundle包-引入bundle-使用bundle
- [sicily]1636. show me the money
- 删除11.2.0.4 rac中 ora.crf的两类文件 和 禁止ora.crf随ohas启动而启动
- 1014. 福尔摩斯的约会 (20)
- 【HDOJ】3436 Queue-jumpers
- C++STL学习(10)STL算法(1)
- MVC IIS创建过程问题收录
- html5通过特殊链接:打电话,发短信,发邮件