Pagini recente » Cod sursa (job #1671118) | Cod sursa (job #2772828) | Cod sursa (job #2915863) | Cod sursa (job #988770) | Cod sursa (job #1295365)
// inversmodular
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a, n;
void gcd(int a, int b, int *x, int *y) {
if (b == 0) {
*x = 1;
*y = 0;
} else {
int x0, y0;
gcd(b, a%b, &x0, &y0);
*x = y0;
*y = x0 - (a/b)*y0;
}
}
void read() {
f>>a>>n;
int xs, ys; // xs = inversul modular
gcd(a, n, &xs, &ys);
if (xs <= 0) {
xs = n + xs%n;
}
g<<xs<<'\n';
}
int main() {
read();
f.close(); g.close();
return 0;
}