Pagini recente » Cod sursa (job #998023) | Cod sursa (job #2499173) | Cod sursa (job #1957093) | Cod sursa (job #183184) | Cod sursa (job #1458455)
//Invers Modular
#include <fstream>
using namespace std;
void EuclidExtended(int a, int b, int &d, int &x, int &y)
{
if(!b)
{
d = a;
x = 1;
y = 0;
}
else
{
int x0,y0;
EuclidExtended(b,a%b,d,x0,y0);
x = y0;
y = x0 - (a/b) * y0;
}
}
int main()
{
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a,n,d,x,y;
f>>a>>n;
EuclidExtended(a,n,d,x,y);
while(x < 0 )
{
x+=n;
}
g<<x % n ;
return 0;
}