#include #define INF 4611686018427387903ll using namespace std; using ll = long long; vector > prime_factorize(ll n){ vector > ret = {}; ll f = 2; while(f * f <= n){ if(n % f == 0){ n /= f; int cnt = 1; while(n % f == 0){ n /= f; ++cnt; } ret.push_back({f, cnt}); } ++f; } if(n != 1){ ret.push_back({n, 1}); } return ret; } ll legendre(ll N, ll K, ll p, int c){ ll ret = 0; ll R = N - K; while(N){ N /= p; ret += N; } while(K){ K /= p; ret -= K; } while(R){ R /= p; ret -= R; } return ret / c; } int main(){ ll N, K, M; cin >> N >> K >> M; vector > primes = prime_factorize(M); ll ans = INF; for(auto&[p, c] : primes){ ll x = legendre(N, K, p, c); if(ans <= x){ continue; } ans = x; } cout << ans << endl; return 0; }