Luo Gu P1082 congruence equation solution to a problem

Topic links: https://www.luogu.com.cn/problem/P1082

Subject to the effect:
ask about \ (x \) the smallest positive integer congruence equation ax≡1 of (mod b) solution.
Tell you \ (a, b \) seeking \ (the X-\) .

Problem-solving ideas:
direct expansion units GCD template.

Codes are as follows:

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

Guess you like

Origin www.cnblogs.com/quanjun/p/12152278.html