扩展欧几里得(exgcd模板)

洛谷P1082

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int exgcd(int a, int b, int &x, int &y)
{
	if(b == 0) {
		x = 1, y = 0;
		return a;
	}
	int ret = exgcd(b, a % b, x, y);
	int temp = x;
	x = y;
	y = temp - a / b * y;
	return ret;
}
int main()
{
	int a, b, x, y;
	cin >> a >> b;
	int ret = exgcd(a, b, x, y);
	x = (x % b + b) % b;
	cout << x;
return 0;
}
原文地址:https://www.cnblogs.com/xuanfly/p/11823516.html