Cod sursa(job #2500302)

Utilizator TeodorLuchianovTeo Luchianov TeodorLuchianov Data 27 noiembrie 2019 18:17:37
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>

using namespace std;

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

typedef long long ll;

ll modulo;

ll cmmdc(ll a, ll b, ll &x, ll &y){
  if(b == 0){
    x = 1;
    y = 0;
    return a;
  }else{
    ll ans = cmmdc(b, a % b, x, y);
    ll xPrim = x, yPrim = y;
    x = yPrim % modulo;
    y = (xPrim - (a / b) * yPrim) % modulo;
    return ans;
  }
}

int main()
{
  ll n, m, x, y, ans;
  in >> n >> m;
  modulo = m;
  ans = cmmdc(n, m, x, y);
  x = x % m;
  if(x < 0)
    x += m;
  out << x;
  return 0;
}