Cod sursa(job #3237855)

Utilizator tsg38Tsg Tsg tsg38 Data 13 iulie 2024 18:33:49
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

void euclid_ext( int a, int b, int &x, int &y ) {
  if ( b == 0 ) {
	x = 1, y = 0;
	return;
  }
  int px, py;
  euclid_ext(b, a % b, px, py);
  x = py;
  y = px - (a / b) * py;
}

int main() {
  ios_base::sync_with_stdio(0);
  fin.tie(0);
  int a, n, x, y;

  fin >> a >> n; 
  euclid_ext(a, n, x, y);
  fout << ((ll)x % n + n) % n;
  fin.close();
  fout.close();
  return 0;
}