Cod sursa(job #2664354)

Utilizator 2016Teo@Balan 2016 Data 28 octombrie 2020 15:41:30
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>
using namespace std;
#define f1 "inversmodular.in"
#define f2 "inversmodular.out"
ifstream in(f1);
ofstream out(f2);
void eclext(int a, int b, int *d, int *x, int *y) {
    if (b == 0) {
        *d = a;
        *x = 1;
        *y = 0;
    } else {
        int x0, y0;
        eclext(b, a % b, d, &x0, &y0);
        *x = y0;
        *y = x0 - (a / b) * y0;
    }
}
int main() {
    int a, n, d, x, y;
    in >> a >> n;
    eclext(a, n, &d, &x, &y);
    if(x > 0)
        out << x;
    else
        out << n + x % n;
    return 0;
}