Cod sursa(job #2233234)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 22 august 2018 17:37:51
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n, M;

void gcd(ll &x, ll &y, ll a, ll b)
{
    if(b == 0){
        x = 1;
        y = 0;
    }else{
        gcd(x, y, b, a%b);
        ll aux = x;
        x = y;
        y = aux - y*(a/b);
    }
}

ll invers(ll x)
{
    ll a, b;
    gcd(a, b, x, M);
    if(a < 0)
        a = M+a%M;
    return a;
}

int main()
{
    ifstream fin ("inversmodular.in");
    ofstream fout ("inversmodular.out");
    fin >> n >> M;
    fout << invers(n) << "\n";
    return 0;
}