Cod sursa(job #2181272)

Utilizator PopeangaMihneaPopeanga Mihnea- Stefan PopeangaMihnea Data 21 martie 2018 16:10:40
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

int x, y, a, n;

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



int main()
{
    fin>>a>>n;
    gcd(a, n, x, y);
    if(x<=0) x=n+x%n;
    fout<<x<<"\n";
    return 0;
}