Pagini recente » Cod sursa (job #95775) | Cod sursa (job #1294139) | Cod sursa (job #1930684) | Cod sursa (job #1030620) | Cod sursa (job #2433772)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in ("inversmodular.in");
ofstream out("inversmodular.out");
int a, n, i, diviz;
int explog(int x1, int n1)
{
int p=1;
int x=x1;
while(n1>0)
{
if(n1&1==1)
{
p=(p*x)%n;
//cout<<p<<" ";
n1--;
}
x=(x*x)%n;
n1>>=1;
}
return p;
}
int ptphi(int x)
{
int temp=x;
for(i=2; i*i<=x; i++)
{
if(x%i==0)
{
while(x%i==0) {x/=i;}
temp=temp/i*(i-1);
}
}
if(x>1) temp=temp/x*(x-1);
return temp;
}
int main()
{
in>>a>>n;
out<<explog(a, ptphi(n)-1);
return 0;
}