Cod sursa(job #991193)

Utilizator cosmyoPaunel Cosmin cosmyo Data 29 august 2013 23:14:27
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <cmath>
using namespace std;

void gcd(int a, int b, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return ;
    }
    int x0, y0;
    gcd(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;
}

int main()
{
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    int a, x, n, y;
    fin >> a >> n;
    gcd(a, n, x, y);
    fout << x << " " << y << '\n';
    int mul = abs(x / n);
    if(x < 0)
    {
        x += (mul + 1) * n;
    }

    fout << x << '\n';

    return 0;
}