Cod sursa(job #2679261)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 30 noiembrie 2020 01:31:23
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

int x,y;
int a,n;

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

int main()
{
    f >> a >> n;
    euclid_extins(a,n);
    if (x<1) {
        int need = x-1;
        need = -need;
        if (need%n==0) {
            x += need;
        }
        else {
            need /= n;
            need++;
            x += need*n;
        }
    }
    g << x;
    return 0;
}