Cod sursa(job #396782)

Utilizator funkydvdIancu David Traian funkydvd Data 15 februarie 2010 21:06:04
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <cstdio>
using namespace std
#define ll long long
ll A, N;
void gcd(ll &x, ll &y, ll a, ll b)  
{  
     if (!b)  
         x = 1, y = 0;  
     else  
     {             
         gcd(x, y, b, a % b);
         ll aux = x;
         x = y;
         y = aux - y * (a / b);
     }
}
int main()
{
    ll inv = 0, ins; 
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);
    scanf("%lld %lld", &A, &N);
    gcd(inv, ins, A, N);
    if (inv <= 0)
       inv = N + inv % N;  
    printf("%lld\n", inv);
      
    return 0;
}