Cod sursa(job #1096059)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 1 februarie 2014 14:39:51
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
/*Invers modular - log(N)
daca gcd(A,N)=1
A*X=1 (mod N ) X e inversul lui A
din gcd extins avem oricare are fi A,N
exista X,Y a.i. AX+BY=gcd(A,B)
deci exista X,y a.i. AX+NY=1
AX+NY=1 (mod N ) <=> AX=1 (mod N)
*/
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

long long N,A,X,inv;

long long  GCD(long long A,long long B,long long &x,long long &y)
{
     if(!B)
     {
          x=1;
          y=0;
          return A;
     }
     long long x0,y0,D;
     D=GCD(B, A % B , x0,y0);
     x=y0;
     y=x0-(A/B)*y0;
     return D;
}
int main()
{
     f>>N>>A;
     GCD(A,N,X,inv);
     while(inv<=0)inv+=N;
     g<<inv<<'\n';
     //g<<X<<'\n';
     return 0;
}