Cod sursa(job #2152423)

Utilizator MaxTeoTeo Oprescu MaxTeo Data 5 martie 2018 15:35:40
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

long long a,n;

int gcdExtended(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }

    long long x0,y0;
    long long gcd=gcdExtended(b,a%b,x0,y0);
    x=y0;
    y=x0-(a/b)*y0;
    return gcd;
}

void modularInverse(long long a,long long n)
{
    long long x,y;
    long long gcd=gcdExtended(a,n,x,y);
    long long res=(x%n+n)%n;
    g<<res;
}

int main()
{
    f>>a>>n;
    modularInverse(a,n);
    return 0;
}