#Q165. 「一本通 5.3 例 1」Amount of Degrees

「一本通 5.3 例 1」Amount of Degrees

Description

Original source: NEERC 2000 Central Subregional, problem statement available at Ural 1057.

Find the number of integers in the given interval [X,Y][X, Y] that satisfy the following condition: the number is exactly equal to the sum of KK distinct integer powers of BB. For example, given X=15X=15, Y=20Y=20, K=2K=2, B=2B=2, there are exactly three numbers that meet the condition:

$$\begin{align} 17&=2^4+2^0 \\ 18&=2^4+2^1 \\ 20&=2^4+2^2 \end{align} $$

Input Format

The first line contains two integers XX and YY. The next two lines contain integers KK and BB.

Output Format

Output a single integer representing the count of numbers that satisfy the condition.

Sample 1

15 20
2
2

3

Data Range and Hint

For all data, 1XY23111\le X\le Y\le 2^{31}-1, 1K201\le K\le 20, 2B102\le B\le 10.