题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669

Romantic

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

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

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
 
Author
yifenfei
题解: 扩展欧几里得算法,数论内容,简单证明一下,gcd(x,y,&a,&b)要求的是ax+by = gcd(x,y);
在求出gcd(y,x%y,a1,b1)的情况下,即a1y + b1x%y = gcd(x,y);
有a1y + b1(x-(x/y)*y) = gcd(x,y) 有a = b1, b = a1-b1(x/y);
那么如果写成gcd(y,x%y, b1,a1) 那么在递归回溯的时候有a = a1,b = b1-a1(x/y)那么就可以不对a再处理,b = b-a(x/y) 即可
这道题要求了x必需是正数,那么就在x的基础上每次都加上一组数,使得x变大,y 变小,使得ax + by = k 不变,既设x = x+x', y = y+y';
那么有ax'+by' = 0; 所以可以取x' = b, y' = -a;
下面是代码:
 #include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
ll gcd(ll a, ll b)
{
return b==? a: gcd(b,a%b);
}
ll extend_gcd(ll x, ll y, ll &a, ll &b)
{
if(y==){
a = ;
b = ;
return x;
}
ll d = extend_gcd(y, x%y, b, a);
b = b - (x/y)*a;
return d;
}
int main()
{
ll a, b;
while(~scanf("%I64d%I64d",&a,&b))
{
if(gcd(a,b)!=)
puts("sorry");
else {
ll x, y;
extend_gcd(a,b,x,y);
while(x<=){
x+=b;
y-=a;
}
printf("%I64d %I64d\n",x,y);
}
}
return ;
}

hdu_2669 Romantic(扩展欧几里得)的更多相关文章

  1. HDU 2669 Romantic (扩展欧几里得定理)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. hdu 2669 Romantic 扩展欧几里得

    Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisf ...

  3. hdu2669-Romantic-(扩展欧几里得定理)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  5. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  6. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  7. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  8. 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

    Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...

  9. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

随机推荐

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. 使用VMware安装linux虚拟机以及相关配置

    前言 使用VMware安装虚拟机这个一般都知道,操作简单.而本文主要讲使用虚拟机的后续相关配置.并记录使用过程中遇到的问题以及一些技巧.本篇文章以后回持续更新的... 安装包准备 VM:12 Linu ...

  3. treeview插件使用:根据子节点选中父节点

    鄙人公司没有专门的前端,所以项目开发中都是前后端一起抡.最近用bootstrap用的比较频繁,发现bootstrap除了框架本身的样式组件外,还提供了多种插件供开发者选择.本篇博文讲的就是bootst ...

  4. sql经典试题

    1.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...

  5. Jmeter+Ant+Jenkins接口自动化测试(一)_环境部署

    前言: 2017年最后一个月份,今天抽出时间把之前的一些记录分享出来,也为今年画上个简单的句号吧,无论好与坏,无论成功与失败,简单的记忆,不要留下点点空白. 特别提示: 知识是用来分享的,但是也要尊重 ...

  6. 安装spark单机环境

    (假定已经装好的hadoop,不管你装没装好,反正我是装好了) 1 下载spark安装包 http://spark.apache.org/downloads.html 下载spark-1.6.1-bi ...

  7. php逐行读取txt文件写入数组的方法

    使用说明: 采用fopen 方法,逐行读取数据,并使用feof($fp)  判断是否文件截止,最后通过filter() 方法,去除空白行,得到所需数据 $file = fopen("user ...

  8. Python的变量和常量

    解释器执行Python的过程:   (python3,c:/test.py) 1:启动python解释器(内存中). 2:将c:/test.py内容从硬盘读到内存中(这一步和文本编辑器是一样的). 3 ...

  9. jsonp及cors

    一. jsonp实现原理是利用script标签可以获取不同源资源的特点,来达到跨域访问某个资源的目的.具体行为如下: 创建一个script标签,将请求地址写入它的src属性,将这个script外链插入 ...

  10. ul li内的文字水平居中显示

    head><style rel="stylesheet" type="text/css" >#top{height:140px;}#top u ...