Pagini recente » Cod sursa (job #1225046) | Cod sursa (job #163753) | Cod sursa (job #2215200) | Rating Robert Peternel (Robert1433) | Cod sursa (job #2870089)
#include <iostream>
#include <fstream>
using namespace std;
const string filename = "inversmodular";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");
void euclid_extins(int a, int b, long long &x, long long &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
euclid_extins(b, a % b, x, y);
long long ax = x, ay = y;
x = ay;
y = ax - (a / b) * ay;
}
int main()
{
bool invers = 0;
int a, b, c;
long long x, y;
fin >> a >> b;
if(a < b)
swap(a, b), invers = 1;
euclid_extins(a, b, x, y);
if(invers)
swap(x, y), swap(a, b);
while(x < 0)
x += b;
fout << x;
return 0;
}