Pagini recente » Rating Andrei Nicolae (_andreinicolae_) | Cod sursa (job #533091)
Cod sursa(job #533091)
#include <algorithm>
#include <fstream>
using namespace std;
const char Input[] = "inversmodular.in";
const char Output[] = "inversmodular.out";
int A, N;
void EucEx( int a, int b, int &d, int &x, int &y ) {
int x0, y0;
if( b == 0 ) {
d = a;
x = 1;
y = 0;
}
else {
EucEx( b, a % b, d, x0, y0 );
x = y0;
y = x0 - (a / b) * y0;
}
}
int main() {
ifstream fin( Input );
ofstream fout( Output );
int d, x, y;
fin >> A >> N;
EucEx( A, N, d, x, y );
while( x < 0 )
x += N;
fout << x % N;
fin.close();
fout.close();
return 0;
}