Cod sursa(job #846388)
Utilizator | Data | 2 ianuarie 2013 00:05:46 | |
---|---|---|---|
Problema | Invers modular | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.58 kb |
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
#define Max 100001
int a,n;
void Euclid(int a,int b,int &d,int &x,int &y)
{
if(b==0)
{
d=a;
x=1;
y=0;
} else
{
int x0,y0;
Euclid(b,a%b,d,x0,y0);
x=y0;
y=x0-(a/b)*y0;
}
}
int main()
{
int d,x,y;
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
scanf("%d %d",&a,&n);
Euclid(a,n,d,x,y);
if(x<0)x=x+(x/n+1)*n;
printf("%d\n",x);
return 0;
}