題目傳送
擴充歐幾裡得闆子題
用來求方程的整數解,由于這裡求的是最小正整數解,是以求出來特解再利用通解公式判斷一下就可以了
擴充歐幾裡得
AC代碼
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
void gcd(ll a,ll b,ll& d,ll& x,ll& y)
{
if(!b) {d = a;x = 1;y = 0;}
else {gcd(b,a%b,d,y,x);y -= x*(a/b);}
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll a,b,x,y,d;
cin >> a >> b;
gcd(a,b,d,x,y);
if(x < 0)
{
for(int i = 0;x <= 0;i++)
{
x = x + i*b;
}
}
else
{
for(int i = 0;;i++)
{
if(x < 0)
{
x += b;
break;
}
else
x = x - i*b;
}
}
//這裡可以直接寫成: x = (x%b + b)%b
cout << x << endl;
}