Cod sursa(job #2673718)

Utilizator andreibazavanAndrei Bazavan andreibazavan Data 17 noiembrie 2020 16:15:32
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long x1,y1;
int p,n;
void cmmdc(int a,int b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
    }

    else
    {
        long long x0;
        long long y0;
        cmmdc(b,a%b,x0,y0);
        x=y0;
        y=x0-(a/b)*y0;
    }
}
int main()
{
    fin>>p>>n;
    cmmdc(p,n,x1,y1);
    if(x1<=0)x1=n+x1%n;
    fout <<x1 << '\n';
    return 0;
}