Cod sursa(job #1689291)

Utilizator sorynsooSorin Soo sorynsoo Data 14 aprilie 2016 09:15:40
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
//
//  main.cpp
//  03invers_modular
//
//  Created by Sorin Sebastian Mircea on 4/14/16.
//  Copyright © 2016 Sorin Sebastian Mircea. All rights reserved.
//

#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;
    
    
}