Cod sursa(job #2543277)
Utilizator | Data | 10 februarie 2020 23:31:47 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.54 kb |
#include <cstdio>
using namespace std;
FILE *f,*g;
void inv(long long a,long long b,long long &x,long long &y)
{
if(!b)
x=1,y=0;
else
{
inv(b,a%b,x,y);
int aux=x;
x=y;
y=aux-a/b*y;
}
}
int main()
{
f=fopen("inversmodular.in","r");
g=fopen("inversmodular.out","w");
long long nr,mod,x,y;
fscanf(f,"%lld %lld",&nr,&mod);
inv(nr,mod,x,y);
if(x<=0)
x=x+mod;
fprintf(g,"%lld\n",x);
fclose(f);
fclose(g);
return 0;
}