題目連接:https://www.luogu.com.cn/problem/P1082c++
題目大意:
求關於 \(x\) 的同餘方程 ax≡1(mod b) 的最小正整數解。
告訴你 \(a,b\) 求 \(x\)。spa
解題思路:
直接套擴展GCD模板。code
實現代碼以下:ci
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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); } } ll inv(ll a , ll n) { ll d , x , y; gcd(a , n , d, x , y); return d == 1 ? (x+n)%n : -1; } ll a, b; int main() { cin >> a >> b; ll c = inv(a, b); cout << c << endl; return 0; }