Cod sursa(job #1819973)

Utilizator emanuel_rRamneantu Emanuel emanuel_r Data 1 decembrie 2016 00:21:08
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include<iostream>
#include<fstream>

using namespace std;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

long long x, y;
long long a, n;

void Inverse(long long a, long long b, long long & x, long long & y)
{
    if(b == 0){
        x = 1;
        y = 0;
        return;
    }
    Inverse(b, a%b, x, y);
    swap(x, y);
    y = y - (a / b)*x;
}

int main()
{
    f>>a>>n;
    Inverse(a, n, x, y);

    if(x < 0)
        x = n + x % n;

    g<<x<<"\n";

    return 0;
}