/*
* url: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1045
* stratege: 求ax+by=gcd(a,b), |x|+|y|的和的x,y,的解, 扩展欧几里得,分四种情况考虑
* status:10541367 10104 Euclid Problem Accepted C++ 0.176 2012-08-30 05:31:09
* Author: johnsondu
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std ;
#define LL long long
void Euclid (LL a, LL b, LL &d, LL &x, LL &y)
{
if (b == 0)
{
d = a ;
x = 1 ;
y = 0 ;
return ;
}
Euclid (b, a%b, d, x, y) ;
LL tmp = x ;
x = y ;
y = tmp - a/b*y ;
}
LL gcd (LL a, LL b)
{
return b ? gcd (b, a%b) : a ;
}
int main ()
{
#if 0
freopen ("date.txt", "r", stdin) ;
#endif
LL a, b, d, x, y, i, c ;
LL x0, y0 ;
LL ax, ay ;
LL tx, ty, mins ;
while (scanf ("%lld%lld", &a, &b) != EOF) // 扩展欧几里得,求ax+by = Gcd (a,b)的最小正整数解
{ //分为4种情况.x的最小正整数,y的最小正整数,x-周期,y-周期
Euclid (a, b, d, x, y) ; //x,y 为 特解, d为a,b 的最大公约数
x0 = (x%b + b) % b ; //x的最小正整数解 (1)
y0 = (d-a*x0)/b ; //相应的y值
mins = abs (x0) + abs(y0) ;
tx = x0, ty = y0 ;
x0-=b ; //x的最小正整数解-x的周期 (2)
y0 = (d-a*x0)/b ; //此时相应的y
if (abs(x0) + abs(y0) < mins)
{
mins = abs(x0) + abs(y0) ;
tx = x0 ;
ty = y0 ;
}
y0 = (y%a + a) % a ; //y的最小正整数解 (3)
x0 = (d-b*y0)/a ; //此时对应的x
if (abs(x0) + abs(y0) < mins)
{
mins = abs(x0) + abs(y0) ;
tx = x0 ;
ty = y0 ;
}
y0 -= a ; //y的最小正整数解-y的周期
x0 = (d-b*y0)/a ; //此时对应的x
if (abs(x0) + abs(y0) < mins)
{
mins = abs(x0) + abs(y0) ;
tx = x0 ;
ty = y0 ;
}
printf ("%lld %lld %lld\n", tx, ty, d) ;
}
return 0 ;
}
相关推荐
Tartancalib标定OAK-FFC_4p(上)
1.使用Tartancalib标定双目或多目相机前,先对单个相机标定获得内参,再获得外参。
uva 10104 Euclid Problem (数论-扩展欧几里德)
Euclid Problem The Problem From Euclid it is known that for any positive integersAandBthere exist such integersXandYthatAX+BY=D, whereDis the greatest comm...
Keil 丢失编译器版本5、内核文件core_cm3.c报错
若用的是旧版本的core_cm3.c ,就使用编译器v5编译工程;若想用编译器v6,就更新4个相关的内核文件:(CMSIS:Cortex Microcontroller Software Interface Standard,ARM Cortex™ 微控制器软件接口标准)
UVa 10104 - Euclid Problem
题目:求同余方程的最小解,扩展的欧几里得。 分析:数论。扩展的欧几里得算法。 说明:第一次写这个╮(╯▽╰)╭。 #include #include #include #include #include #include using namespace std; int gcd(int a, int b, int &x, int &y) { if (a%b == 0) {
13、基因相似性度量的相关性及特征选择研究
本文研究了基因相似性度量和特征选择在生物学中的应用,详细介绍了GO术语相似性度量的多种方法,包括语义度量和路径长度度量,并分析了不同方法在酵母和人类数据集上的表现。研究发现,Avg-sum方法和基于路径长度的度量在多数情况下效果较好。此外,通过晚期辐射毒性预测的案例研究,探讨了特征选择的重要性及可能面临的过拟合和噪声干扰问题,并提出了相应的解决策略。
Linux | Ubuntu18.04安装WIFI6 AX201驱动完整教程
本节介绍了如何在Ubuntu18下安装AX201驱动的教程。
UVA10104 Euclid Problem【扩展欧几里得算法】
From Euclid it is known that for any positive integers A and B there exist such integers X and Y thatAX + BY = D, where D is the greatest common divisor of A and B. The problem is to find for givenA a...
AI问数,智慧赋能:基于大语言模型的数据播报员应用实践探索
AI问数技术正在改变企业与数据交互的方式,它以自然语言为桥梁,让数据不再是少数专家的专利,而是成为所有业务人员的决策伙伴。这不仅是技术的创新,更是管理理念的革新。在数字化转型的关键阶段,企业应积极拥抱AI问数技术,通过降低数据使用门槛、提升数据利用效率、释放数据潜在价值,实现真正的数据驱动决策,为企业的高质量发展注入源源不断的智慧动能。AI问数,正在开启一个数据普惠的新时代。
UVa-10104-Euclid Problem
The Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX+BY=D, where D is the greatest common divisor of A and B. The problem is to find
手把手教你学Simulink实例:基于Simulink的锂电池管理系统(BMS)仿真
是确保锂电池安全、高效运行的关键组件。BMS 主要功能包括监控电池的状态(如电压、电流、温度)、估算荷电状态(State of Charge, SOC)、健康状态(State of Health, SOH),以及实现电池均衡和保护机制等。Simulink 提供了一个强大的仿真环境,可以用来设计、分析和验证锂电池管理系统的控制策略。常见的均衡方法包括被动均衡(通过电阻放电)和主动均衡(通过开关电容或变压器)。通过本教程,我们详细介绍了如何使用 Simulink 进行锂电池管理系统的仿真实例。
【教程4>第8章>第10节】OFDM完整通信链路项目的FPGA开发4——插入导频
本文介绍了基于MATLAB和Verilog的导频插入设计与实现方法。首先回顾了MATLAB中导频插入的算法实现,展示了每32个数据插入一个导频的设计。接着详细说明了Verilog硬件实现方案,包括导频序列定义、计数器设计、FIFO模块等关键部分。通过仿真测试验证了设计的正确性,输出了插入导频后的序列、导频序列以及调整时序后的序列。最后提供了相关视频教程资源,帮助读者理解设计过程。该设计实现了数据与导频的精确插入,为OFDM等通信系统开发提供了实用参考。
uva10104
//今天又学了好多东西,呵呵 //已知A,B //求X,Y使得AX+BY=gcd(A,B) //使|X|+|Y|最小 //用扩展欧几里得: #include int extend_gcd(int a,int b,int &x,int &y) { if(b==0) { x=1;y=0; return a; } int ans=exte
15、分布式查询处理详解
本博文深入探讨了分布式环境下的查询处理与优化技术。从查询处理的基本概念出发,分析了在分布式系统中执行查询所面临的额外挑战,并通过实例比较了不同的执行策略。文章详细介绍了查询优化的组成要素,包括搜索空间、成本模型和搜索策略,同时涵盖了查询处理的多个层次以及数据本地化的核心方法。此外,还讨论了连接排序优化、分布式成本模型以及多种查询优化方法,如静态优化、动态优化和混合优化。最后,提出了自适应查询处理的概念,旨在提高分布式查询的灵活性和效率。通过综合应用这些技术,可以有效提升分布式数据库的查询性能,降低通信和计算
UVa Problem 10104 Euclid Problem (欧几里德问题)
// Euclid Problem (欧几里德问题) // PC/UVa IDs: 110703/10104, Popularity: A, Success rate: average Level: 1 // Verdict: Accepted // Submission Date: 2011-06-10 // UVa Run Time: 0.684s // // 版权所有(C)2011,邱秋。m...
IMSL CNL 7.0.0 (x86-64)Crack
IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
uva10104 - Euclid Problem
Euclid Problem The Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y thatAX+BY=D, where D is the greatest common divisor of A and
假人有限元模型
假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用
uva 10104 (扩展欧几里德算法)
今天粗略学习了扩展欧几里德算法。 设a,b,c为任意整数,g=gcd(a,b),方程 ax+by=g的一组解是(x1,y1),则当c是g的倍数时,ax+by=c的一组解是(x1*c/g,y1*c/g);当c不是g的倍数时无整数解。 UVA 10104: he Problem From Euclid it is known that for any positive intege
迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)
迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)
UVA10104 Euclid Problem【数论 扩展欧几里得算法】
题目链接:10104 - Euclid Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX + BY = D, where D is the greatest common divisor of A and B. The p...
uva 10104 - Euclid Problem
#include #include using namespace std; long gcd(long a, long b, long *x, long *y) { long tx, ty; long g; if (b > a) return gcd(b, a, y, x); if (b == 0) { *x =
杭电DP题目(dynamic programming)
<br />这里集中了很多的动态规划题目,我也从这里开始突破啦<br />----------------------------------------------------------------------------------------------<br /> <br /> 记我今天开始,正式向ACM营地出发,努力搞好自己的CSDN博客<br /> <br /> turn potential to reality !<br />
HDU 1312 Red and Black
看了好多天的DFS,基本上的程序都是要借助解题报告才可以实现 但是这一题,确实依照自己的思路,完全靠自己写出来的,或许比较简单吧,所以特此发表下文章,犒劳一下自己,再接再厉 http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red
杭电1051 经典贪心
这题真是抓狂,我已经提交了8次,还是没过,我都找不到任何错误数据了粘贴下别人的代码提交下,就是果断过。。。求大牛指点……/* title:Wooden Sticks url: http://acm.hdu.edu.cn/showproblem.php?pid=1051 我的做题思想: 主要是通过下表法,首先对数组的length按从小到大排序 然后,通过第一元素开始,进行比较,如果后面的任一 一组数据与当前数据进行比较,length和weight都比目前 length 和 weight 都小
hdu 1930 And Now, a Remainder from Our Sponsor 扩展欧几里得 解一元线性同余方程组
/** * url : http://acm.hdu.edu.cn/showproblem.php?pid=1930 * stratege : 解一元线性同余方程组, 扩展欧几里得 * Author: johnsondu * Status: johnsondu 0MS 284K 2207B C++ 2012-08-19 13:13:15 * Trick: There will be n
LightOj 1111 - Best Picnic Ever
/** * Author: johnsondu * time: 2013-4-11 * problem: LightOj 1111 - Best Picnic Ever * url: http://lightoj.com/volume_showproblem.php?problem=1111 * stratege: DFS, count the person who can r
POJ 3641 HDU 1905 Pseudoprime numbers 素数 模取幂
/* --------------------------------------------------- status : a312745658 3641 Accepted 328K 16MS C++ 1732B 2012-05-18 21:36:37 stratege : 快速模取幂,素数筛选, 判断一个大数是否为素数 URL: http:
HDU 1211 RSA 逆元 快速模取幂
/** * url: http://acm.hdu.edu.cn/showproblem.php?pid=1211 RSA * Author: Johnsondu * Time: 2012-7-28 18:00 around * Stratege: 逆元(扩展欧几里德) , 快速模取幂 * Status: 6364213 2012-07-28 18:12:54 Accepte
HDU 1010 Tempter of the Bone
关于DFS如何学好,先援引大牛的几点建议: (1)先理解搜索的原理,区别好bfs和dfs的优先搜索方向以及应用,例如bfs一般用来求最优解如最短路径. (2)掌握好所用的数据结构,特别是队列. (3)牢记dfs和bfs的常用模式,它是基本固定的,考试时信手拈来! (4)注
1283



被折叠的 条评论
为什么被折叠?



