Cod sursa(job #624058)
| Utilizator | Data | 21 octombrie 2011 17:04:56 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include<fstream>
using namespace std;
int A,N;
int ModN(int x)
{
int a=x%N;
if(a<0)
return (a+N);
return a;
}
void Euclid_extins(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;
y=0;
return;
}
else
{
Euclid_extins(b,a%b,x,y);
int aux,q=a/b;
aux=x;
x=y;
y=aux-y*q;
}
}
int main()
{
int inv=0,aux=0;
ifstream fin("inversmodular.in");
fin>>A>>N;
fin.close();
Euclid_extins(A,N,inv,aux);
ofstream fout("inversmodular.out");
fout<<ModN(inv)<<"\n";
fout.close();
return 0;
}
