Cod sursa(job #1853408)

Utilizator alexnekiNechifor Alexandru alexneki Data 21 ianuarie 2017 19:02:52
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a, n;

void gcd(long &x, long &y, long a, long b) {
//  cout << x << " " << y << " " << a << " " << b << endl;
  if (!b)
    x = 1, y = 0;
  else {
    gcd(x, y, b, a % b);
    long aux = x;
    x = y;
    y = aux - y * (a / b);
//    cout << x << " " << y << " " << a << " " << b << endl;
  }
}

int main() {
  long invers = 0, y = 0;

  in >> a >> n;
  gcd(invers, y, a, n);

  if (invers <= 0)
    invers = n + invers % n;

  out << invers << endl;
  return 0;
}