Cod sursa(job #1033011)

Utilizator TheNechizFMI Razvan Birisan TheNechiz Data 16 noiembrie 2013 12:35:57
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <stdio.h>

#define ll long long

int A, N;

void gcd(ll &x, ll &y, int a, int 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("%d %d", &A, &N);
    gcd(inv, ins, A, N);

    if (inv <= 0)
       inv = N + inv % N;

    printf("%lld\n", inv);

    return 0;
}
/*
# include <fstream>
# define ll long long
using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int A,N;

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

int main()
{
    ll X,Y;
    fin >> A >> N;
    euclid(X,Y,A,N);

    if( X <= 0 ) X = N + X%N;

    fout << X;

    fin.close();
    fout.close();
    return 0;
}
*/