Cod sursa(job #1689311)

Utilizator sorynsooSorin Soo sorynsoo Data 14 aprilie 2016 09:33:59
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");

long long x, y;
int a,b;

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

int main()
{
    cin >> a >> b;
    invers_modular(x, y, a, b);
    
    if( x < 0 )
        x = b + x%b;

    cout << x;
    return 0;
}