Cod sursa(job #2645610)

Utilizator etohirseCristi Cretu etohirse Data 29 august 2020 09:36:32
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
/**
 *    author: etohirse
 *    created: 29.08.2020 09:33:18
 **/
#include <fstream>
using ll = long long;

std::ifstream fin("inversmodular.in");
std::ofstream fout("inversmodular.out");

int a, n;

void ecl(ll &x, ll &y, int a, int b) {
  if (!b) {
    x = 1, y = 0;
  } else {
    ecl(x, y, b, a % b);
    ll aux = x;
    x = y;
    y = aux - y * (a / b);
  }
}

int main() {
  ll inv = 0, ins;
  fin >> a >> n;
  ecl(inv, ins, a, n);
  if (inv <= 0) {
    inv = n + inv % n;
  }
  fout << inv;
  return 0;
}