Cod sursa(job #3198782)
| Utilizator | Data | 30 ianuarie 2024 15:29:33 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.53 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long a,n,x,y;
void inversModular(long long a, long long b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;
y = 0;
}
else
{
long long xx, yy;
inversModular(b, a%b, xx, yy);
x = yy;
y = xx-(a/b)*yy;
}
}
int main()
{
fin >> a >> n;
inversModular(a, n, x, y);
if (x < 0)
x = (x+n*((-x)/n+1))%n;
fout << x;
return 0;
}