Cod sursa(job #2784436)

Utilizator sandifx68Fazakas Alexandru sandifx68 Data 16 octombrie 2021 14:42:23
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <cstdio>

using namespace std;

FILE *f=fopen("inversmodular.in","r");
FILE *g=fopen("inversmodular.out","w");

void euclid(long long a, long long b, long long &d, long long &x, long long &y){
    if(!b){
        d=a;
        x=1;
        y=0;
        return;
    }
    long long x1,y1;
    euclid(b,a%b,d,x1,y1);
    x=y1;
    y=x1-y1*(a/b);
}

int main()
{
    long long a,b,d,x,y;
    fscanf(f,"%lld%lld",&a,&b);
    euclid(a,b,d,x,y);
    if(x<0)
        x=b+x%b;
    fprintf(g,"%lld",x);

}