#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; vector factorList(lint n) { vector V; if (!(n % 2)) { while (!(n % 2)) { V.push_back(2); n /= 2; } } int rn = sqrt(n); vector v(rn+1); for(lint i = 3; i <= rn && n > 1; i += 2) { if(!v.at(i)) { if (!(n % i)) { while (!(n % i)) { V.push_back(i); n /= i; } } v.at(i) = 1; for(lint j = i*i; j <= rn; j += i) { v.at(j) = 1; } } } if (n != 1) V.push_back(n); return V; } int main() { lint N, K, M; cin >> N >> K >> M; vi v = factorList(M); lint a=0, b=0, c=0, x=0, y, z=1; lint ans = LINF; map m; rep(i, v.size()) m[v[i]]++; for (auto p : m) { tie(x, y) = p; z = 1; lint t = 0; while (z < LINF/x) { z *= x; a = N/z; b = K/z; c = (N-K)/z; t += a-b-c; } chmin(ans, t/y); } std::cout << ans << '\n'; }