local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs local ffi = require("ffi") local C = ffi.C ffi.cdef[[ long long atoll(const char*); ]] local function lltonumber(str) return C.atoll(str) end local function getprimes(x) local primes = {} local allnums = {} for i = 1, x do allnums[i] = true end for i = 2, x do if allnums[i] then table.insert(primes, i) local lim = mfl(x / i) for j = 2, lim do allnums[j * i] = false end end end return primes end local function getdivisorparts(x, primes) local prime_num = #primes local tmp = {} local lim = mce(msq(x)) local primepos = 1 local dv = primes[primepos] while primepos <= prime_num and dv <= lim do if x % dv == 0 then local cnt = 1 x = mfl(x / dv) while x % dv == 0 do x = mfl(x / dv) cnt = cnt + 1 end tmp[dv] = cnt lim = mce(msq(x)) end if primepos == prime_num then break end primepos = primepos + 1 dv = primes[primepos] end if x ~= 1 then tmp[x] = 1 end return tmp end local n, k, m = io.read():match("(%d+) (%d+) (%d+)") n = lltonumber(n) k = lltonumber(k) m = tonumber(m) local primes = getprimes(1000 * 1000) local dvp = getdivisorparts(m, primes) local ret = false for p, cnt in pairs(dvp) do local tmp = n local pl = 1LL * p local z = 0LL while 0LL < tmp do z = z + tmp / pl tmp = tmp / pl end tmp = k while 0LL < tmp do z = z - tmp / pl tmp = tmp / pl end if k < n then tmp = n - k while 0LL < tmp do z = z - tmp / pl tmp = tmp / pl end end if not ret then ret = z elseif z < ret then ret = z end end ret = tostring(ret):gsub("LL", "") print(ret)