def cont(x): ret = 0 div = M while x >= div: ret += x // div div *= M return ret N,K,M = map(int,input().split()) ans = cont(N) - cont(K) - cont(N-K) print (ans)