Cod sursa(job #2420040)

Utilizator SergiuS3003Sergiu Stancu Nicolae SergiuS3003 Data 10 mai 2019 11:55:35
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 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)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        d = a;
    }
    else
    {
        int x0, y0;
        EuclidExtins(b, a % b, d, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}
int InversModular(int A, int N)
{
    int X, Y, d;
    EuclidExtins(A, N, d, X, Y);
    X %= N;
    if(X < 0)
        X += N;
    return X;
}
int main()
{  int a,b;
   f>>a>>b;
   g<<InversModular(a,b);
    return 0;
}