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