Cod sursa(job #578420)

Utilizator bogfodorBogdan Fodor bogfodor Data 11 aprilie 2011 11:52:26
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <cstdio>

using namespace std;

FILE *fin=freopen("inversmodular.in","r",stdin);
FILE *fout=freopen("inversmodular.out","w",stdout);

int a,n;
void inv(int a, int n, long long &x, long long &y)
{
    if(!n)
        x=1,y=0;
    else
    {
        inv(n,a%n,x,y);
        long long aux=x;
        x=y;
        y=aux-(a/n)*y;
    }
}

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