#P3286. [SCOI2014] 方伯伯的商场之旅
[SCOI2014] 方伯伯的商场之旅
Description
One day, Uncle Fang went to a game hosted by a mall. The mall arranged some staff members in a line. In front of each person, there are several piles of stones.
By coincidence, for the person at position , the number of stones in the -th pile is exactly the -th digit of when written in base . Now Uncle Fang is going to play a game: the mall will give him two integers .
Uncle Fang needs to merge all the piles in front of each person whose position is in into a single pile. In each operation, he may choose two piles in front of one person and move some stones from one pile to the other. The cost is equal to (the number of stones moved) (the distance moved).
The mall promises that as long as Uncle Fang completes the task, they will give him some coconuts; the smaller the cost, the more coconuts he will receive. So he is anxious and asks you to tell him the minimum cost. For example, for the person at position in base , the minimum cost to merge the stones is $1 \times 2 + 2 \times 1 + 3 \times 0 + 1 \times 1 + 2 \times 2 = 9$, that is, merge all stones into the third pile.
Input Format
The input consists of only line containing space-separated integers , representing the two integers given by the mall and the base.
Output Format
Output only line containing integer, the minimum cost.
3 8 3
5
Hint
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号