Cod sursa(job #1676375)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 5 aprilie 2016 21:21:38
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int a,n;

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

int main()
{
    in>>a>>n;
    in.close();
    int x,y;
    int d = euler(a,n,x,y);
    x*=d;
    y*=d;
    while(x<0)
        x+=n;
    out<<x<<"\n";
    out.close();
    return 0;
}