Pagini recente » Cod sursa (job #393834) | Monitorul de evaluare | Cod sursa (job #2856371) | Cod sursa (job #1910838) | Cod sursa (job #1973854)
#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 (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 b,ll e) {
ll ans = 1;
while (e) {
if (e&1) {
ans = (ans * b) % N;
}
b = (b * b) % N;
e >>= 1;
}
return ans;
}