Cod sursa(job #1958083)
Utilizator | Data | 7 aprilie 2017 23:59:44 | |
---|---|---|---|
Problema | Invers modular | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.4 kb |
#include <cstdio>
using namespace std;
typedef long long ll;
ll a, n, d, x, y;
ll gcd(ll a, ll b, ll &x, ll &y)
{
if(!b)
{
x = 1, y = 0;
return a;
}
ll x1, y1;
ll d = gcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return d;
}
int main()
{
scanf("%lld%lld", &a, &n);
d = gcd(a, n, x, y);
if(x <= 0)
x = n + x % n;
printf("%lld", x);
return 0;
}