N,K,M = map(int,input().split()) def f(l,r,x): al = (l + pow(M,x) -1) // pow(M,x) al = max(al,1) ar = r //pow(M,x) if ar >= 1 and al <= ar: return ar - al + 1 else: return 0 def g(l,r): x = 1 res = 0 while pow(M,x) <= r: res += f(l,r,x) x += 1 return res up = g(N-K+1,N) down = g(1,K) print(up-down)