n, k, m = map(int,input().split()) def count(x): #1*2*...*xをmで割り切る回数 ret = 0 rui = 0 for i in range(1000, -1, -1): now = x // (m ** i) ret += now rui += now return ret print(count(n) - count(k) - count(n - k))