Cod sursa(job #3298524)

Utilizator KapetaneAvram Armand-Florin Kapetane Data 30 mai 2025 20:43:41
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

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

int main() {
    long long A,N;
    fin>>A>>N;
    long long x,y;
    long long sol=euclid_extins(A,N,x,y);

    if(sol!=1)
    {
        fout<<"Err";  
        return 1;
    } 
    else
    {
        x=(x%N+N)%N; 
        fout<<x;
    }

    fin.close();
    fout.close();
    return 0;
}