Pagini recente » Cod sursa (job #830082) | Cod sursa (job #887640) | Cod sursa (job #166657) | Cod sursa (job #1650375) | Cod sursa (job #2016907)
#include <fstream>
#include <vector>
using namespace std;
int fv[50001];
int modulo;
vector <int> ciur;
void ciurulet ()
{
for (int i = 2; i<=50000; ++i)
{
if (fv[i] == 0)
{
ciur.push_back(i);
for (int j = i+i; j<=50000; j+=i)
fv[j] = 1;
}
}
}
int putere (long long a, long long b)
{
long long raspuns = 1;
a%=modulo;
while (b)
{
if (b%2)
{
raspuns = raspuns*a%modulo;
--b;
}
else
{
a = a*a%modulo;
b/=2;
}
}
return raspuns;
}
int main()
{
ifstream fin ("inversmodular.in");
ofstream fout ("inversmodular.out");
long long n, a, fi;
fin >> a >> n;
modulo = n;
fi = n;
ciurulet();
for (auto x:ciur)
{
if (n%x == 0)
{
fi = fi/x*(x-1);
while (n%x == 0)
n/=x;
}
}
if (n > 1)
fi = fi/n*(n-1);
fout << putere (a, fi-1);
return 0;
}