Cod sursa(job #2295380)

Utilizator isav_costinVlad Costin Andrei isav_costin Data 3 decembrie 2018 17:01:35
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <cstdio>

using namespace std;

int gcd( int a, int b, int &x, int &y )
{
  if( b==0 )
  {
    x=1;
    y=0;

    return a;
  }

  int d, x0, y0;

  d=gcd(b,a%b,x0,y0);

  x=y0;
  y=x0-y0*(a/b);

  return d;
}

int main()
{
  freopen( "inversmodular.in", "r", stdin );
  freopen( "inversmodular.out", "w", stdout );

  int a, b; // a*x + b*y =1

  scanf( "%d%d", &a, &b );

  int d, x, y;

  d=gcd(a,b,x,y);

  while( x<0 )
    x+=b;

  printf( "%d", x );

  return 0;
}