Cod sursa(job #1968977)
Utilizator | Huhurez Marius trutruvasilica | Data | 18 aprilie 2017 06:17:51 |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.5 kb |
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void gcd(int &x,int &y,int a,int b)
{
if(b==0)
{
x=1;
y=0;
}
else
{
int x0,y0;
gcd(x0,y0,b,a%b);
x=y0;
y=x0-(a/b)*y0;
}
}
int invmod(int A,int mod)
{
int x,y;
gcd(x,y,A,mod);
if(x<0) x=mod+x%mod;
return x;
}
int main()
{
int A,mod;
fin>>A>>mod;
fout<<invmod(A,mod);
}