Cod sursa(job #1199020)

Utilizator hasmasandragosHasmasan Dragos hasmasandragos Data 17 iunie 2014 22:57:35
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <fstream>
using namespace std;

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

void invmod (int a, int b, int &x, int &y)
{
    if (!b)
    {
      x=1;
      y=0;
    }
    else
    {
        int x0,y0;
        invmod (b, a%b, x0,y0);
        x=y0;
        y=x0-(a/b)*y0;
    }
}

int A,N;

int main()
{
    f>>A>>N;
    int x,y;
    invmod(A,N,x,y);
    if (x<=0) x=N+x%N;
    g<<x<<'\n';
    return 0;
}