首页 > 编程语言 > 详细

数论:扩展欧几里得算法

时间:2019-08-03 16:03:25      阅读:112      评论:0      收藏:0      [点我收藏+]

_______________________我不知道将去何方,但我已经在路上。

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片(下取整!)

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

 

Summary:

技术分享图片

技术分享图片

技术分享图片

技术分享图片技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

codes:

#include<iostream>
#include<algorithm>
using namespace std;
int a,b,d,x,y;
int exgcd(int a,int b,int& x,int& y)
{
	if(b==0) 
	{
		x=1;
		y=0;
		return a;
	}
	int t=exgcd(b,a%b,x,y);
	int x0=x;
	int y0=y;
	x=y0;
	y=x0-(a/b)*y0;
	return t; 
}

int main()
{
	while(cin>>a>>b>>d && a && b && d)
	{
		int t=__gcd(a,b);
		a/=t;b/=t;d/=t;
		exgcd(a,b,x,y);
		x=d*x;
		y=d*y;
		
		int tx=(x%b+b)%b;//为神木模b? 
		int ty=(d-a*tx)/b;
		if(ty<0) ty=-ty;
		
		y=(y%a+a)%a;
		x=(d-b*y)/a;
		if(x<0) x=-x;
		
		if(x+y>tx+ty) 
		{
			x=tx;
			y=ty;
		}
		cout<<x<<‘ ‘<<y<<endl;
	}
	return 0;
} 

  Reference: https://blog.csdn.net/u013008291/article/details/38359073

 

数论:扩展欧几里得算法

原文:https://www.cnblogs.com/dragondragon/p/11294839.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!