hdu 2669 Romantic
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
Each case two nonnegative integer a,b (0<a, b<=2^31)
Output
Sample Input
77 51
10 44
34 79
Sample Output
2 -3
sorry
7 -3
分析:ax+by=gcd(a,b)*k,只有k为整数时,才有解,所以当前仅当gcd(a,b)=1时,这道题有解。
当求解出x<0时,利用
(x+b/gcd(a,b),y-a/gcd(a,b) 都是符合条件的解这一性质,找到第一个符合条件的x输出之。
#include<iostream>
#include<stdio.h>
using namespace std;
long long ext_gcd(long long a,long long b,long long *x,long long *y)
{
if(b==)
{
*x=;
*y=;
return a;
}
long long r = ext_gcd(b,a%b,x,y);
long long t = *x;
*x=*y;
*y=t - a/b * *y;
return r;
}
int main()
{ long long a,b,x,y;
while(~scanf("%I64d%I64d",&a,&b))
{
if(ext_gcd(a,b,&x,&y)==)
{
while(x<)
{
x+=b/;
y-=a/;
} printf("%I64d %I64d\n",x,y);
continue;
}
else printf("sorry\n");
}
return ;
}
hdu 2669 Romantic的更多相关文章
- hdu 2669 Romantic (乘法逆元)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic 扩展欧几里德---->解不定方程
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic (扩展欧几里得定理)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic(裸的拓展欧几里得)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic(扩展欧几里德)
题目链接:pid=2669">http://acm.hdu.edu.cn/showproblem.php?pid=2669 Problem Description The Sky is ...
- HDU 2669 Romantic【扩展欧几里德】
裸的扩展欧几里德,求最小的X,X=((X0%b)+b)%b,每个X都对应一个Y,代入原式求解可得 #include<stdio.h> #include<string.h> ty ...
- HDU 2669 Romantic(扩展欧几里德, 数学题)
题目 //第一眼看题目觉得好熟悉,但是还是没想起来//洪湖来写不出来去看了解题报告,发现是裸的 扩展欧几里得 - - /* //扩展欧几里得算法(求 ax+by=gcd )//返回d=gcd(a,b) ...
- hdu 2669 Romantic 扩展欧几里得
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisf ...
- HDU 2669 Romantic( 拓欧水 )
链接:传送门 题意:求解方程 X * a + Y * b = 1 的一组最小非负 X 的解,如果无解输出 "sorry" 思路:裸 exgcd /***************** ...
随机推荐
- glib-2.49.4 static build step in windows XP
export LIBFFI_CFLAGS=" -I/usr/local/lib/libffi-3.2.1/include " \ export LIBFFI_LIBS=" ...
- Java for LeetCode 224 Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 括号配对问题_栈<stack>
问题 A: 括号配对问题 时间限制: 3 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版] 题目描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行 ...
- jq获取绝对定位与相对定位的top, left值
jquery 实现方法绝对var X = $('#ID').offset().top; var Y = $('#ID').offset().left; 相对var X = $('#ID').posit ...
- jQueryUI Datepicker的使用
jQueryUI Datepicker是一个高度可定制的插件,可以很方便的为你的页面添加日期选择功能,你可以自定义日期的显示格式 以及要使用的语言. 你可以使用键盘的快捷键来驱动datepicker插 ...
- $.each 和$(selector).each()的区别
$.each() 对数组或对对象内容进行循环处理 jQuery.each( collection, callback(indexInArray, valueOfElement) ) collectio ...
- VS VA助手补丁覆盖目录
以VS2010为例. VA_X.dll 复制到以下文件夹内,覆盖原文件. WinXP系统: %USERPROFILE%\Local Settings\Application Data\Microsof ...
- 240个jquery插件(转)
http://www.kollermedia.at/archive/2007/11/21/the-ultimate-jquery-plugin-list/File upload Ajax File U ...
- SQL Server多表多列更新
student表: lag表: 要求将student表stu_id列为1的stu_nick列和stu_phont列的数据更新为lag表的lag_nick列和lag_phone列. SQL语句: upd ...
- Java网络连接之HttpURLConnection 与 HttpClient
HttpClient使用详解:http://blog.csdn.net/wangpeng047/article/details/19624529 注:HttpURLConnection输出流用ou ...