Pagini recente » Cod sursa (job #1349164) | Cod sursa (job #1893105) | Cod sursa (job #238793) | Cod sursa (job #333149) | Cod sursa (job #933903)
Cod sursa(job #933903)
#include<fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, b, x, y, c, d;
void euclid(int a, int b, int &x, int &y, int &d){
if(b == 0){
d = a;
x = 1;
y = 0;
return;
}
int xx, yy, q;
q = a/b;
euclid(b, a%b, xx, yy, d);
x = yy;
y = xx - q*yy;
}
int main(){
int c = 1;
fin >> a >> b;
euclid(a, b, x, y, d);
int q = c/d;
x = x*q;
while(x < 1)
x+= b;
while(x > b-1)
x-= b;
fout << x;
fin.close();
fout.close();
return 0;
}