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

思路:exgcd 最后X为最小正解

代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if(!b) {
x=;y=;return a;
}
ll ans=exgcd(b,a%b,x,y);
ll temp=x;
x=y;
y=temp-a/b*y;
return ans;
}
int main() {
ios::sync_with_stdio(false);
ll a,b,x,y;
while(cin>>a>>b) {
ll g=exgcd(a,b,x,y);
if(g!=) {
cout<<"sorry"<<endl;
} else {
x=(x%b+b)%b;
y=(-a*x)/b;
cout<<x<<" "<<y<<endl;
}
}
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 扩展欧几里德---->解不定方程

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

  3. HDU 2669 Romantic(扩展欧几里德, 数学题)

    题目 //第一眼看题目觉得好熟悉,但是还是没想起来//洪湖来写不出来去看了解题报告,发现是裸的 扩展欧几里得 - - /* //扩展欧几里得算法(求 ax+by=gcd )//返回d=gcd(a,b) ...

  4. hdu 5512 Pagodas 扩展欧几里得推导+GCD

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

  5. hdu_2669 Romantic(扩展欧几里得)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 Romantic Time Limit: 2000/1000 MS (Java/Others)  ...

  6. HDU A/B 扩展欧几里得

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).   Input 数据的 ...

  7. 扩展欧几里得 hdu 1576

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...

  8. hdu 1573 A/B (扩展欧几里得)

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...

  9. hdu 1576 A/B 【扩展欧几里得】【逆元】

    <题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...

随机推荐

  1. highcharts框架使用总结

    Highcharts官网地址:https://www.hcharts.cn/products/highcharts首先需要引入jQuery框架,然后包含Highcharts框架需要使用到的js文件,最 ...

  2. C#读写Shapefile

    Shapefile文件是ArcGIS存储矢量要素的标准格式,要读写Shapefile最简单的方法当然是基于ArcObject(或者ArcEngine)开发,不过网上也有一些开源的解译Shapefile ...

  3. 使用JS实现图片轮播滚动跑马灯效果

    我的第一篇文章.哈哈.有点小鸡冻.  之前在百度搜索"图片轮播"."图片滚动",结果都是那种可以左右切换的.也是我们最常见的那种.可能是搜索 关键字的问题吧. ...

  4. Leetcode题解(25)

    77. Combinations 题目 分析:求给定数字n,k的组合数,方法是采用深度搜索算法,代码如下(copy网上代码) class Solution { public: void dfs77(v ...

  5. C. New Year and Rating

    C. New Year and Rating time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. eclipse禁用svg文件Validation

    1.打开window>preferences>validation找到xml validator 2.点击xml validator最右侧的按钮打开xml校验规则窗口,选中exclude ...

  7. SSH框架的多表查询和增删查改 (方法一)上

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的 ...

  8. 学习笔记TF061:分布式TensorFlow,分布式原理、最佳实践

    分布式TensorFlow由高性能gRPC库底层技术支持.Martin Abadi.Ashish Agarwal.Paul Barham论文<TensorFlow:Large-Scale Mac ...

  9. Python 解LeetCode:367. Valid Perfect Square

    题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...

  10. [转载] gitbook安装与使用

    转载自http://blog.csdn.net/xiaocainiaoshangxiao/article/details/46882921 废话不说,直接主题: gitbook安装 ========= ...