HDU 2669 Romantic 拓展欧几里得模板题

初学欧几里得,帮助理解。。

这里的x要为最小正整数,求出x然后又等式得到y。

 

#include
#include
using namespace std;
typedef long long LL;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
	if(!b) {d=a,y=0,x=1;}
	else
	{
		gcd(b,a%b,d,y,x);
		y-=x*(a/b);
	}
}
int main()
{
//	freopen("E:\\ACM\\test.txt","r",stdin);
	LL a,b,d,x,y,c=1;
	while(cin>>a>>b)
	{
		gcd(a,b,d,x,y);
		
		if(c%d) puts("sorry"); //c不是d的倍数则没有整数解 
		else
		{
			x*=c/d; //得到特解
			b/=d;
			x=(x%b+b)%b; //求最小正整数解x 
			y=(1-a*x)/b; //由等式推出y 
			cout<

 

 

 

 

 

你可能感兴趣的:(算法)