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

题意:给你两个数a,b,让你找到一个非负的整数x和一个整数y,使得ax+by=1,如果有多种情况,x要取最小的。ps:图还是挺好看的(笑)

思路:这是普通的欧几里德模板题,其中要使得x最小,那么先把特解x0求出来,那么最小的x就是(x0%b+b)%b.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
ll extend_gcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;return a;
}
ll d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll niyuan(ll a,ll n){
ll x,y;
ll d=extend_gcd(a,n,x,y);
if(d==1) return (x%n+n)%n;
else return -1;
}
ll gcd(ll a,ll b){
return b ? gcd(b,a%b) : a;
} int main()
{
ll n,m,b,d,a,x,y;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
if(gcd(a,b)!=1){
printf("sorry\n");continue;
}
d=extend_gcd(a,b,x,y);
ll x1;
x1=(x%b+b)%b;
ll t=(x1-x)/b;
printf("%lld %lld\n",x1,y-a*t);
}
return 0;
}

hdu2669Romantic (扩展欧几里德)的更多相关文章

  1. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  2. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  3. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  4. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

  5. CF 7C. Line(扩展欧几里德)

    题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...

  6. poj2142-The Balance(扩展欧几里德算法)

    一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...

  7. poj2115-C Looooops(扩展欧几里德算法)

    本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...

  8. poj1061-青蛙的约会(扩展欧几里德算法)

    一,题意: 两个青蛙在赤道上跳跃,走环路.起始位置分别为x,y. 每次跳跃距离分别为m,n.赤道长度为L.两青蛙跳跃方向与次数相同的情况下, 问两青蛙是否有方法跳跃到同一点.输出最少跳跃次数.二,思路 ...

  9. HDU 1576 A/B【扩展欧几里德】

    设A/B=x,则A=Bx n=A%9973=A-9973*y=Bx-9973*y 用扩展欧几里德求解 #include<stdio.h> #include<string.h> ...

随机推荐

  1. 剑指offer 面试题0:高质的代码:即考虑边界条件、特殊输入和错误处理

    Q:把一个字符串转换为整数. A1:一个普通但漏洞百出的解法. int StrToInt(char* str) { int number = 0; while (*str != 0) { number ...

  2. LeetCode448-数组中消失的数字

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

  3. 使用nodejs和express搭建http web服务

    目录 简介 使用nodejs搭建HTTP web服务 请求nodejs服务 第三方lib请求post 获取http请求的正文 Express和使用express搭建http web服务 express ...

  4. linux源码安装软件的一般方法

    rhel系统貌似安装不了xmgrace,配置的时候居然说要那个M*tif库.百度了一下,需要openmotif库,然后用root账户想要用yum安装一下这个库,搞了好久没搞懂.后面搞明白了,原因竟是因 ...

  5. explain select * from xuehao;

    mysql> explain select * from xuehao;+----+-------------+--------+------+---------------+------+-- ...

  6. zabbix 监控的数据

    /usr/local/zabbix/bin/zabbix_sender --zabbix-server 192.168.1.10 --port 10051 --input-file /var/log/ ...

  7. CTFshow-萌新赛杂项_签到

    查看网页信息 http://game.ctf.show/r2/ 把网页源码下载后发现有大片空白 使用winhex打开 把这些16进制数值复制到文件中 把20替换为0,09替换为1后 得到一串二进制数值 ...

  8. Linux删除文件后磁盘目录不释放

    今天测试oracle数据库的时候,把表空间连带内容和数据文件一并删除了,但是删除之后,查看数据文件不存在了,但是目录的带下没有释放 SQL> drop tablespace users incl ...

  9. 登陆到 SAP 系统后的用户出口

    增强类型:smod 增强名称:SUSR0001 组件(退出功能模块):EXIT_SAPLSUSF_001 功能:用户每次登陆SAP系统后都会调用这个SUSR0001增强,可以在FUNCTION EXI ...

  10. CentOS 7 下安装 mysql ,以及用到的命令

    VMware虚拟机装好后,再装个CentOS7系统,以上环境自行百度... 一.Linux下查看mysql是否安装 1.指令ps -ef|grep mysql [root@localhost 桌面]# ...