Pagini recente » Cod sursa (job #1511478) | Cod sursa (job #1456313) | Cod sursa (job #1065973) | Cod sursa (job #3216286) | Cod sursa (job #1959294)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <sstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
typedef long long ll;
ll A,N;
ll getPhi(ll);
ll pw(ll,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 x) {
ll ans = x;
for (ll i=2;i*i <= x;++i) {
if (x % i != 0) {
continue;
}
while (x % i == 0) {
x /= i;
}
ans = ans * (i-1) / i;
}
if (x != 1) {
ans = ans * (x-1) / x;
}
return ans;
}
ll pw(ll b,ll e) {
ll ans = 1;
while (e) {
if (e & 1) {
ans = (ans * b) % N;
}
b = (b*b) % N;
e >>= 1;
}
return ans;
}