Cod sursa(job #2021448)
Utilizator | Data | 13 septembrie 2017 18:36:53 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.62 kb |
#include <iostream>
#include <cstdio>
using namespace std;
void prog(long long &x, long long &y, int a, int b)
{
if (b==0)
x = 1, y = 0;
else
{
prog(x, y, b, a % b);
long long aux = x;
x = y;
y = aux - y * (a / b);
}
}
int main()
{
int a,n;
long long invers = 0, rest;
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
scanf("%d %d", &a, &n);
prog(invers, rest, a, n);
if (invers<=0)
invers = n + invers % n;
printf("%lld\n", invers);
return 0;
}