Cod sursa(job #1589717)

Utilizator gapdanPopescu George gapdan Data 4 februarie 2016 12:45:37
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <cstdio>

using namespace std;
int a,n;

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

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