Pagini recente » Cod sursa (job #810959) | Cod sursa (job #63433) | Cod sursa (job #1708668) | Cod sursa (job #557626) | Cod sursa (job #2050838)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
const int NMax = 1e5 + 5;
const ll inf = 1e18 + 5;
ll A,N;
ll pw(ll,ll);
ll getPhi(ll);
int main() {
in>>A>>N;
ll phi = getPhi(N);
out<<pw(A,phi-1)<<'\n';
in.close();out.close();
return 0;
}
ll getPhi(ll nr) {
ll ans = nr;
for (ll i=2;i*i <= nr;++i) {
if (nr % i != 0) {
continue;
}
while (nr % i == 0) {
nr /= i;
}
ans = ans * (i-1) / i;
}
if (nr != 1) {
ans = ans * (nr - 1) / nr;
}
return ans;
}
ll pw(ll base,ll exp) {
ll ans = 1;
while (exp) {
if (exp & 1) {
ans = (ans * base) % N;
}
base = (base * base) % N;
exp >>= 1;
}
return ans;
}