Cod sursa(job #1403523)

Utilizator cautionPopescu Teodor caution Data 27 martie 2015 12:55:56
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.48 kb
#include <fstream>
using namespace std;
void euclid(long a, long b, long *x, long *y)
{
    if(b==0)
    {
        *x=1;
        *y=0;
    }
    else
    {
        long x0, y0;
        euclid(b, a%b, &x0, &y0);
        *x=y0;
        *y=x0-(a/b)*y0;
    }
}
int main()
{
    ifstream in("inversmodular.in");
    ofstream out("inversmodular.out");
    long a, n, x, y;
    in>>a>>n;
    euclid(a, n, &x, &y);
    while(x<0) x+=n;
    out<<x<<endl;
    return 0;
}