Cod sursa(job #2900827)

Utilizator cadmium_Voicu Mihai-Valeriu cadmium_ Data 12 mai 2022 11:04:30
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

#ifndef DLOCAL
  #define cin fin
  #define cout fout
  ifstream cin("inversmodular.in");
  ofstream cout("inversmodular.out");
#endif

void gcd(int a, int b, int& x, int& y, int& d) {
  if(b == 0) {
    x = 1, y = 0, d = a;
    return; 
  }
  int x0, y0;
  gcd(b, a % b, x0, y0, d);
  x = -y0;
  y = (a / b * y0 - x0);
  // cerr << a << ' ' << b << ' ' << x << ' ' << y << ' ' << d << '\n';
}

// b * x0 + (a - a / b * b) * y0 = a * x0 + b * y0;


signed main() {
  int A, N;
  cin >> A >> N;
  int x, y, d;
  gcd(A, N, x, y, d);
  cout << x << '\n';
}