其他分享
首页 > 其他分享> > 扩展欧几里得

扩展欧几里得

作者:互联网

P1082

#include<bits/stdc++.h>
using namespace std;
#define file(a) freopen(#a".in","r",stdin),freopen(#a".out","w",stdout)
int exgcd(int a,int b,int &x,int &y)
{
	if(b==0)
	{
		x=1;y=0;
		return a;
	}
	int d=exgcd(b,a%b,x,y);
	int tmp=x;
	x=y;
	y=tmp-(a/b)*y;
	return d;
} 
bool liEu(int a,int b,int c,int &x,int &y)
{
	int d=exgcd(a,b,x,y);
	if(c%d) return 0;
	int k=c/d;
	x*=k;y*=k;
	return 1;  
}
int main()
{
	int a,b,x=0,y=0;
	scanf("%d%d",&a,&b);
	if(liEu(a,b,1,x,y))
	{
		printf("%d",(x%b+b)%b);
	}
	return 0;
}


标签:tmp,return,int,欧几里得,扩展,exgcd,freopen,liEu
来源: https://www.cnblogs.com/cbdsopa/p/15930536.html