//愚直に素因数分解 & 最大ケース埋め込み #include #include using namespace std; using ll = long long; template using V = vector; template using VV = V >; const ll inf = 1000000000000000000; VV prime_factorize(ll n) { if(n == (ll)999999999989){ return {{(ll)999999999989,1}}; } VV prime = {}; ll f = 2; while (f <= n) { if (n % f == 0) { n /= f; ll cnt = 1; while (n % f == 0) { n /= f; cnt++; } prime.push_back({ f,cnt }); } else { f++; } } if (n != 1) { prime.push_back({ n,1 }); } return prime; } ll kummer(ll N, ll K, V& p) { ll R = N - K; ll c = 0; ll cnt = 0; while (R or K) { ll a = R % p[0]; ll b = K % p[0]; if (a + b + c >= p[0]) { cnt++; c = (a + b + c) / p[0]; } else { c = 0; } R /= p[0]; K /= p[0]; } return cnt / p[1]; } int main(void) { ll N, K, M; cin >> N >> K >> M; VV prime = prime_factorize(M); ll ans = inf; for (auto& p : prime) { ans = min(ans, kummer(N, K, p)); } cout << ans << endl; return 0; }