Cod sursa(job #2086927)

Utilizator andreistanStan Andrei andreistan Data 12 decembrie 2017 18:03:48
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

void EuclidExtins(int a, int b, int &d, int &x, int &y)
{
    int x1 = 0, y1 = 1;
    x = 1, y = 0;
    while (b != 0)
    {
        int q = a / b;
        int r = a % b;
        a = b;
        b = r;
        int x0 = x - x1 * q;
        int y0 = y - y1 * q;
        x = x1;
        y = y1;
        x1 = x0;
        y1 = y0;
    }
    d = a;
}

int main()
{
    int A, N, X, Y, d;
    f >> A >> N;
    EuclidExtins(A, N, d, X, Y);
    X %= N;
    if (X < 0)X += N;
    g << X;
    return 0;
}