Cod sursa(job #2356905)

Utilizator dimi999Dimitriu Andrei dimi999 Data 26 februarie 2019 23:17:02
Problema Invers modular Scor 100
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 a,b;

void euclid_extins(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
        x=1,y=0;
    else
    {
        euclid_extins(b,a%b,x,y);
        long long aux=y;
        y=x-(a/b)*y;
        x=aux;
    }
}

int main()
{
    long long m,n,x,y;
    fin>>n>>m;
    euclid_extins(n,m,x,y);
    if(x<=0)
        x=m+x%m;
    fout<<x;
    return 0;
}