Cod sursa(job #1300235)
Utilizator | Data | 24 decembrie 2014 10:54:51 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.43 kb |
#include <iostream>
#include <fstream>
using namespace std;
int n, p, x, y, nr;
void ex(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;
y=0;
}
else
{
ex(b,a%b,x,y);
nr=x;
x=y;
y=nr-(a/b)*y;
}
}
int main()
{
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
cin>>n>>p;
ex(n,p,x,y);
if(x<=0)
x=p+x%p;
cout<<x;
return 0;
}