Pagini recente » Cod sursa (job #1640519) | Cod sursa (job #462606) | Cod sursa (job #2011963) | Cod sursa (job #2596443) | Cod sursa (job #1973852)
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
#define ll long long
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 nr) {
ll ans = nr;
for (int i=2;i * i <= nr;++i) {
if (nr % i != 0) {
continue;
}
while (nr % i == 0) {
nr /= i;
}
ans *= (i-1) / i;
}
if (nr != 1) {
ans = nr * (ans-1) / ans;
}
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;
}