問題への羅区P1082合同方程式ソリューション

トピックリンク:https://www.luogu.com.cn/problem/P1082

:効果の対象
について尋ねる\(X \)(MOD b)の溶液の最小の正の整数合同式ax≡1。
あなたを教えて\(、B \)を求める\(X-を\)

問題解決のアイデア:
ダイレクト拡張ユニットGCDテンプレート。

次のようにコードは次のとおりです。

#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;
}

おすすめ

転載: www.cnblogs.com/quanjun/p/12152278.html