Cod sursa(job #2073075)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 22 noiembrie 2017 18:10:13
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <fstream>

using namespace std;
  
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");

void get_xy(int a, int b, int &x, int &y) {

  if (b == 0) {
    x = 1;
    y = 0;
    return;
  }

  int x0, y0;
  get_xy(b, a % b, x0, y0);
  x = y0; 
  y = x0 - (a / b) * y0;
}

int main(int argc, char const *argv[]) {
  
  int a, n;
  cin >> a >> n;

  int x, y;
  get_xy(a, n, x, y);
  while (x < 0) {
    x += n;
  }

  cout << x << '\n';
}