欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

[exgcd算最值] luogu 3951

程序员文章站 2022-03-23 22:13:18
题目代码#include#include#include#include#include#include#include#include#include#include#include...

题目

[exgcd算最值] luogu 3951

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f;
int x,y;
void exgcd(int a,int b){
	if(b==0){
		x=1;
		y=0;
		return;
	}
	exgcd(b,a%b);
	int tmp=x;
	x=y;
	y=tmp-(a/b)*y;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int a,b;
	cin>>a>>b;
	exgcd(a,b); 
	int tmp=(-x)/b;
	x+=tmp*b;
	y-=tmp*y;
	while(x<0){
		x+=b;
		y-=a;
	}
	int xx=x;
	while(x>0){
		x-=b;
		y+=a;
	}
	int yy=y;
	//cout<<xx<<" "<<yy<<endl;
	cout<<a*(xx-1)+b*(yy-1)-1<<endl;
    return 0;
}

本文地址:https://blog.csdn.net/kosf_/article/details/108125685