Pagini recente » Cod sursa (job #1539911) | Cod sursa (job #2787923) | Cod sursa (job #2132775) | Cod sursa (job #2905733) | Cod sursa (job #2363042)
#include <bits/stdc++.h>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
long long A, N;
void read()
{
f >> A >> N;
}
long long expLg(int x, int p)
{
int y = 1;
while (p > 1) {
if (p & 1)
y = (x * y) % N;
x = (x * x) % N;
p >>= 1;
}
return (x * y) % N;
}
long long phi(long long x)
{
long long res = x;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0) {
while (x % i == 0)
x /= i;
res = (res / i) * (i - 1);
}
if (x > 1)
res = (res / x) * (x - 1);
return res;
}
void show()
{
g << expLg(A, phi(N) - 1);
}
int main()
{
read();
show();
return 0;
}