Cod sursa(job #764864)

Utilizator mi5humihai draghici mi5hu Data 6 iulie 2012 15:41:12
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <stdio.h>
using namespace std;

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

int main(){
    int nr1, nr2, d, x, y;
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);
    
    scanf("%d%d", &nr1, &nr2); 
    euclid(nr1, nr2, &d, &x, &y);
    if (x < 0) {
       x = nr2 + x % nr2;   
    }
    printf("%d", x);
    
    return 0;  
}