Cod sursa(job #3226601)

Utilizator TeodorLuchianovTeo Luchianov TeodorLuchianov Data 22 aprilie 2024 10:52:03
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("inversmodular.in");
ofstream out("inversmodular.out");

typedef long long ll;

ll cmmdc(ll a, ll b, ll &x, ll &y) {
  if(b == 0) {
    x = 1;
    y = 0;
    return a;
  }else {
    ll ans = cmmdc(b, a % b, x, y);
    ll xPrim = x, yPrim = y;
    x = yPrim;
    y = (xPrim - (a / b) * yPrim);
    return ans;
  }
}

int main() {

  ll a, n, x, y, ans = 0;
  in >> a >> n;
  ans = cmmdc(a, n, x, y);
  if(x > 0) {
    out << x % n << '\n';
  }else {
    out << x + ((-x) / n + 1) * n << '\n';
  }
  return 0;
}