Cod sursa(job #1900436)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 3 martie 2017 13:05:20
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <stdio.h>

void InvMod(int a, int b, int &x, int &y){

    if(b == 0){
        x = 1;
        y = 0;
    }else{
        int x0, y0;
        InvMod(b, a % b, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}

int main(){

    int A, N, x, y;

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

    scanf("%d %d", &A, &N);
    InvMod(A, N, x, y);

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

    printf("%d\n", x);

    return 0;
}