QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 64 MB Points totaux : 100

#14474. Uncle Fang's Shopping Trip

Statistiques

One day, Uncle Fang went to participate in a game held at a mall. The mall had arranged some staff members in a row. Each person had several piles of stones in front of them. Coincidentally, the number of stones in the $j$-th pile in front of the person at position $i$ is exactly the $j$-th digit of $i$ when written in base $K$.

Now, Uncle Fang wants to play a game. The mall gives Uncle Fang two integers $L$ and $R$. Uncle Fang needs to merge all the stones of each person at positions in the range $[L, R]$ into a single pile.

In each operation, he can choose two piles of stones in front of a person and move some stones from one pile to the other. The cost is the number of stones moved multiplied by the distance moved.

The mall promises that as long as Uncle Fang completes the task, they will give him some coconuts; the lower the cost, the more coconuts he gets. Therefore, Uncle Fang is in a hurry and wants you to tell him the minimum cost.

For example: For a person at position $12312$ in base $10$, the minimum cost to merge the stones is: $1 \times 2 + 2 \times 1 + 3 \times 0 + 1 \times 1 + 2 \times 2 = 9$ This means merging all stones into the third pile.

Input

The input consists of a single line containing three space-separated integers $L$, $R$, and $K$, representing the two integers given to Uncle Fang by the mall and the base $K$.

Output

The output consists of a single line containing one integer, representing the minimum cost.

Examples

Input 1

3 8 3

Output 1

5

Constraints

For $10\%$ of the data, $1 \le L \le R \le 10^5$

For $30\%$ of the data, $1 \le L \le R \le 10^7$

For $100\%$ of the data, $1 \le L \le R \le 10^{15}, 2 \le K \le 20$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.