Cod sursa(job #1497701)

Utilizator Burbon13Burbon13 Burbon13 Data 7 octombrie 2015 10:16:06
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <cstdio>
using namespace std;

void euclid(int a, int b, long long &x, long long &y){
    if(not b){
        x = 1;
        y = 0;
        return;
    }
    euclid(b,a%b,x,y);
    long long aux = x;
    x = y;
    y = aux - y * (a/b);
}

int main(){
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);

    int a,n;
    long long x,y;
    scanf("%d %d", &a, &n);
    euclid(a,n,x,y);

    if(x < 0)
        x = n + x % n;

    printf("%lld", x);

    return 0;
}