Pagini recente » Cod sursa (job #2820596) | Cod sursa (job #1618294) | Cod sursa (job #2475974) | Cod sursa (job #2476146) | Cod sursa (job #1290430)
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "inversmodular";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;
const int NMAX = 10000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;
int A, N;
int expLog(int B, int E, int MOD) {
if(E == 0) return 1;
if(E == 1) return B % MOD;
int t = expLog(B, E / 2, MOD);
return ((t * 1LL * t) % MOD * expLog(B, E % 2, MOD)) % MOD;
}
int phi(int x) {
int i, n = x;
lld ans = x;
for(i = 2; i * i <= x; i++)
if(n % i == 0) {
ans = ans / i * (i - 1);
while(n % i == 0)
n /= i;
}
return ans;
}
int invMod(int B, int MOD) {
return expLog(B, phi(MOD) - 2, MOD);
}
int main() {
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
scanf("%d%d", &A, &N);
printf("%d\n", invMod(A, N));
return 0;
}