Cod sursa(job #726702)
| Utilizator | Data | 27 martie 2012 13:55:45 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <iostream>
#include <cstdio>
using namespace std;
int a,b;
long long x,y;
long long inv(long long &x,long long &y,int a,int b)
{
if (!b)
x=1,y=0;
else
{
inv(x,y,b,a%b);
long long aux=x;
x=y;
y=aux-y*(a/b);
}
}
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d%d",&a,&b);
inv(x,y,a,b);
if (x<=0)
x=b+x%b;
printf("%lld",x);
return 0;
}
