from collections import defaultdict, deque, Counter from heapq import heapify, heappop, heappush import math from copy import deepcopy from itertools import combinations, permutations, product, combinations_with_replacement from bisect import bisect_left, bisect_right import sys def input(): return sys.stdin.readline().rstrip() def getN(): return int(input()) def getNM(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getListGraph(): return list(map(lambda x:int(x) - 1, input().split())) def getArray(intn): return [int(input()) for i in range(intn)] mod = 10 ** 9 + 7 MOD = 998244353 # sys.setrecursionlimit(10000000) inf = float('inf') eps = 10 ** (-15) dy = [0, 1, 0, -1] dx = [1, 0, -1, 0] ############# # Main Code # ############# """ x = 0なら余りは0 nCkは求められない どんな数か? Mの因数はいくつあるか?という問題 分子と分母でMの個数を数えてみる nCk = n! / (n - r)!r! とすると簡単 8*7*6*5*4*3*2*1/3*2*1 * 5*4*3*2*1 """ N, K, M = getNM() def calc(x, m): tar = m res = 0 while tar <= x: res += x // tar tar *= m return res print(calc(N, M) - calc(N - K, M) - calc(K, M))