Pagini recente » Cod sursa (job #1091570) | Cod sursa (job #1691616) | Cod sursa (job #830213) | Cod sursa (job #1790568) | Cod sursa (job #1536263)
#include<fstream>
using namespace std;
long long a, n, x, y;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void f(long long a, long long b, long long &x, long long &y){
if(b == 0){
x = 1;
y = 0;
}
else{
long long x1, y1;
f(b, a % b, x1, y1);
x = y1;
y = x1 - a / b * y1;
}
}
int main(){
fin>> a >> n;
f(a, n, x, y);
if(x < 0){
x = ( x + (((-x) / n + 1) * n) ) % n;
}
fout<< x <<"\n";
return 0;
}