uva 10104 (扩展欧几里德算法)
来源:互联网 发布:淘宝订单号 规则 编辑:程序博客网 时间:2024/06/09 18:22
今天粗略学习了扩展欧几里德算法。
设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 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 given A and Bcorresponding X, Y and D.
The Input
The input will consist of a set of lines with the integer numbers A and B, separated with space (A,B<1000000001).
The Output
For each input line the output line should consist of three integers X, Y and D, separated with space. If there are several such X and Y, you should output that pair for which |X|+|Y| is the minimal (primarily) and X<=Y (secondarily).
Sample Input
4 617 17
Sample Output
-1 1 20 1 17
代码:
#include<cstdio>
void gcd(int a,int b,int &d,int &x,int &y)
{
if(b==0)
{
d=a;
x=1;y=0;
}
else
{
gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
using namespace std;
int main()
{
int a,b,d,x,y;
while(scanf("%d%d",&a,&b)!=EOF)
{
gcd(a,b,d,x,y);
printf("%d %d %d\n",x,y,d);
}
return 0;
}
- uva 10104 (扩展欧几里德算法)
- (扩展)欧几里德算法
- (扩展)欧几里德算法
- 欧几里德及扩展欧几里德算法(学习)
- uva 10104 Euclid Problem (数论-扩展欧几里德)
- (扩展)欧几里德得算法
- 欧几里德算法 & 扩展欧几里德算法
- 欧几里德和扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- 欧几里德与扩展欧几里德算法
- .net framework 4.5 独立安装包下载
- jquery中复选框全部选定或者全部不选定
- 用C语言实现面向对象
- 打开设备管理器
- 集群下tomcat间session复制
- uva 10104 (扩展欧几里德算法)
- 经验分享:CSS浮动(float,clear)通俗讲解
- Activity在切换屏幕时的方法调用【转】
- Windows驱动开发:用C还是C++
- for循环时jquery闭包问题
- ibatis与spring整合实例(附源码)
- PowerDesigner的样式设置
- 问题解决:/lib/libEGL.so.1:对‘wl_display_dispatch_queue_pending’未定义的引用
- apache +mod_wsgi + django deploying