Cod sursa(job #2017254)

Utilizator B_RazvanBaboiu Razvan B_Razvan Data 31 august 2017 17:33:09
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <cstdio>

using namespace std;

int a, n;

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

int main()
{
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);
    scanf("%d %d", &a, &n);
    long long x=0, y=0;
    findX(x, y, a, n);
    if(x <= 0)
        x = n + x%n;
    printf("%lld", x);
    return 0;
}