#P2405. non天平

    ID: 1407 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp数学高精度线段树洛谷原创进制

non天平

Description

The weights each have masses that are powers of nn: n1,n2,n3,n4,n5n^1, n^2, n^3, n^4, n^5, and so on. non wants to know the minimum number of weights needed to measure his weight mm. Note that weights can be placed on either the left or the right pan.

Input Format

The first line contains a non-negative integer mm, representing non's weight.

The second line contains a positive integer nn, representing the base of the powers for the weights.

Output Format

Output a single integer representing the minimum number of weights required.

99
10

2

Hint

Constraints

For 30%30\% of the testdata, m2631m \le 2^{63} - 1.

For 100%100\% of the testdata, 0m10100000 \le m \le 10^{10000}, 0<n100000 < n \le 10000.

Translated by ChatGPT 5