Cod sursa(job #2393815)
| Utilizator | Data | 1 aprilie 2019 09:14:23 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
#include <bits/stdc++.h>
using namespace std;
int A, N;
void modularinverse(long long &x, long long &y, int a, int b)
{
if (b == 0)
{
x = 1;
y = 0;
return;
}
modularinverse(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, &N);
long long x = 0, y;
modularinverse(x, y, A, N);
if (x <= 0)
x = N + x % N;
printf("%lld", x);
return 0;
}
