Cod sursa(job #1667805)

Utilizator gabimoiseMoise Gabriel gabimoise Data 29 martie 2016 11:37:36
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <cstdio>

using namespace std;

long a,n,x1,y1;

void det(int a, int b, long &x, long &y)
{
     if (b==0) {x=1; y=0;}
     else
     {
         det(b,a%b,x,y);
         long aux=x;
         x=y;
         y=aux-y*(a/b);
     }
}

int main()
{
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);
    scanf ("%ld%ld",&a,&n);
    det(a,n,x1,y1);
    while (x1<=0) x1=x1%n+n;
    printf("%ld\n",x1);
    return 0;
}