Cod sursa(job #1686667)
| Utilizator | Data | 12 aprilie 2016 12:53:55 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 60 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long a,MOD;
long long inv(long long a)
{
long long s=1;
int p=MOD-2;
while(p)
{
if(p&1)
{
s=1LL*s*a;
int cat=s/MOD;
s-=cat*MOD;
}
a=1LL*a*a;
int cat=a/MOD;
a-=cat*MOD;
p=p>>1;
}
return s;
}
int main()
{
fin>>a>>MOD;
fout<<inv(a);
return 0;
}
