Interval#4902

Interval

1 second
64 megabytes
Hard

Determine the number of integers within the closed interval [X, Y] that can be represented as the sum of distinct powers of ; that is, the number of integers such that each power of is used at most once in the representation:

Z = a_n b^n + a_{n-1} b^{n-1} + \dots + a_1 b^1 + a_0 b^0,

where a_i \in \{0, 1\}.

Input

The first line contains the integers , , and (1 \le X \le Y \le 10^{100}, 2 \le b \le 10), separated by spaces.

Output

Print a single integer — the number of integers in [X, Y] that can be expressed as the sum of distinct powers of .

Examples

Example 1

Loading
Loading

Note

For X = 4, Y = 10, b = 3, the numbers are:

\begin{align*} 4 &= 3^1 + 3^0,\\ 9 &= 3^2,\\ 10 &= 3^2 + 3^0. \end{align*}

For X = 1, Y = 12, b = 2, the numbers are:

\begin{align*} 1 &= 2^0,\\ 2 &= 2^1,\\ 3 &= 2^1 + 2^0,\\ 4 &= 2^2,\\ 5 &= 2^2 + 2^0,\\ 6 &= 2^2 + 2^1,\\ 7 &= 2^2 + 2^1 + 2^0,\\ 8 &= 2^3,\\ 9 &= 2^3 + 2^0,\\ 10 &= 2^3 + 2^1,\\ 11 &= 2^3 + 2^1 + 2^0,\\ 12 &= 2^3 + 2^2. \end{align*}