Cod sursa(job #1790379)

Utilizator ZeBuGgErCasapu Andreas ZeBuGgEr Data 28 octombrie 2016 09:31:27
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include<cstdio>

using namespace std;

void invmod(long long int &x,long long int &y,int a,int b)
{
    if(b==0)
    {
        x=1;
        y=0;
    }
    else
    {
        invmod(x,y,b,a%b);
        long long int temp;
        temp=x;
        x=y;
        y=temp-y*(a/b);
    }
}

int a,n;

int main()
{
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);

    scanf("%d %d",&a,&n);
    long long int x=0,y;
    invmod(x,y,a,n);
    if(x<0)
    {
        x=x%n+n;
    }
    printf("%lld",x);
}