Cod sursa(job #2031556)

Utilizator rangal3Tudor Anastasiei rangal3 Data 3 octombrie 2017 13:56:25
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#define in "inversmodular.in"
#define out "inversmodular.out"

using namespace std;

ifstream fin(in);
ofstream fout(out);

typedef long long ll;

inline void Euclid(int a,int b,int &x,int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        int x0,y0;
        Euclid(b,a%b,x0,y0);

        x = y0;
        y = x0 - a/b*y0;
    }
}

int inv_modular(int a,int mod)
{
    int b = mod;
    int x,y;

    Euclid(a,b,x,y);

    return x;
}

int main()
{
    int A,N,X;
    fin>>A>>N;

    X = inv_modular(A,N);
    while(X < 0)
        X += N;
    fout<<X;

    return 0;
}