Cod sursa(job #2152421)

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

int a,n;

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

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

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

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