Cod sursa(job #578416)

Utilizator bogfodorBogdan Fodor bogfodor Data 11 aprilie 2011 11:51:20
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.48 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, int &x, int &y)
{
    if(!n)
        x=1,y=0;
    else
    {
        inv(n,a%n,x,y);
        int aux=x;
        x=y;
        y=aux-(a/n)*y;
    }
}

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