题意
求解\(ax \equiv 1 \mod b\)

套路变形为\(ax-by=1\)

题目保证有解(即为a、b互质),使用exgcd求解

#include<bits/stdc++.h>
#define int long long
using namespace std;

int a,b;

int exgcd(int A,int B,int &x,int &y){
	if(B==0) return x=1,y=0,A;
	int g=exgcd(B,A%B,x,y);
	int t=x;
	x=y;y=t-A/B*y;
	return g;	
}

signed main(){
	cin>>a>>b;
	int x,y;
	int g=exgcd(a,b,x,y);
	int stp=b/g;
	if(stp<0) stp=-stp;
	x%=stp;
	if(x<=0) x+=stp;
	cout<<x<<endl;
	return 0;
}

相关文章:

  • 2021-10-18
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-02-13
猜你喜欢
  • 2021-05-18
  • 2021-07-28
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案