Romantic HDU - 2669 (Extended Euclidean)

Romantic

Title link: HDU - 2669
title meaning: find a solution of a*x+b*y=1 where x is the smallest non-negative number; no solution output sorry;
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
long long exgcd(long long a, long long b, long long &x, long long &y){
	if(b==0){
		x=1, y=0;
		return a;
	}
	long long r=exgcd(b, a%b, x, y);
	long long t=x;
	x=y;
	y=ta/b*y;
	return r;
}
int main(){
	long long a, b, x, y;
	while(cin >> a >> b){
		if(exgcd(a, b, x, y)==1){	
			if(x<0) x+=b, y-=a;//x is a non-negative number, the smallest;
			cout << x << ' ' << y << endl;
		}
		else cout << "sorry\n";
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774799&siteId=291194637