Pagini recente » Cod sursa (job #3120849) | Cod sursa (job #2685043) | Cod sursa (job #1830947) | Cod sursa (job #547729) | Cod sursa (job #3249815)
#include <bits/stdc++.h>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a, b;
void bezout(long long &x, long long &y, long long &d, int a, int b)
{
if(!b)
x = 1, y = 0, d = a;
else
{
bezout(x, y, d, b, a % b);
///swap(x,y);
x ^= y; y ^= x; x ^= y;
y = y - x * (a / b);
/*
int aux = x;
x = y;
y = aux - y * (a / b);*/
}
}
int main()
{
long long x = 0, y = 0, d;
///Egalitatea lui Bezout:
///Exista x si y astfel incat
/// a * x + b * y = ( a, b) = d
f >> a >> b;
/// functie care implementeaza calcului eg lui bezout
bezout(x, y, d, a, b);/// compelxitate log n
/// 2) Daca a si b sunt prime intre ele, atunci
/// a * x + b * y = 1
/// atunci practic x va fi inversul modular al lui a mod b
if(x <= 0) /// daca inversu modular este negativ trebuie pus cu picioarele pe pamant
x = b - abs(x) % b;
g << x; /// afisam inversul modular
cout << x << '*' << a << '+' << y << '*' << b << '=' << d; /// afisam inegalitatea lui bezout
return 0;
}