Cod sursa(job #1295365)
| Utilizator | Data | 19 decembrie 2014 12:34:17 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
// 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;
}