Cod sursa(job #735854)

Utilizator visanrVisan Radu visanr Data 17 aprilie 2012 13:25:39
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;


long gcd(long a,long b,long &x,long &y)
{
     if(b==0)
     {
             x=1;
             y=0;
             return a;
     }
     long xaux,yaux,d;
     d=gcd(b,a%b,xaux,yaux);
     x=yaux;
     y=xaux-(a/b)*yaux;
     return d;
}


int main()
{
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);
    long a,b,c;
    int i;
    scanf("%ld %ld", &a,&b);
    c=1;
    long d,x,y;
    d=gcd(a,b,x,y);
    if(c%d==0)
    {
         long aux=x*(c/d);
         while(aux<0) aux+=b;
         printf("%ld\n", aux);
    }
    scanf("%i", &i);
    return 0;
}