Cod sursa(job #1684002)
| Utilizator | Data | 10 aprilie 2016 18:31:55 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.49 kb |
#include <iostream>
#include <fstream>
using namespace std;
int a,n;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
void calc(long long &x,long long &y,int a1,int a2)
{
if(!a2){x=1; y=0;}
else
{
calc(x,y,a2,a1%a2);
long long aux=x;
x=y; y=aux-y*(a1/a2);
}
}
void solve()
{
long long ins=0,inv; f>>a>>n;
calc(inv,ins,a,n);
if(inv<=0)inv=n+inv%n;
g<<inv;
}
int main()
{
solve();
return 0;
}
