問題一覧 > 通常問題

No.2287 ++ -- *=a /=a

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 20
作問者 : みここ / テスター : cureskol sapphire__15 👑 potato167
お気に入りにしたユーザー ProblemId : 9170 / 自分の提出
問題文最終更新日: 2023-04-26 18:34:42
++ -- *=a /=a
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
yukicoder contest 386 (順位表) の他の問題:

Problem Statement

You are given non-negative integers $x, y$ and an integer $a$ that is at least $2$.

Initially, $x$ is written on a blackboard. You may perform any of the following operations any number of times (possibly zero).

  • Replace the integer $z$ written on the blackboard with $z + 1$.
  • Replace the integer $z$ written on the blackboard with $z - 1$. However, this operation cannot be performed when $z = 0$.
  • Replace the integer $z$ written on the blackboard with $z \times a$.
  • Replace the integer $z$ written on the blackboard with $\lfloor \frac{z}{a} \rfloor$.

Find the minimum number of operations required to make the integer written on the blackboard equal to $y$.

You are given $T$ test cases; find the answer for each of them.

Input

$T$
test$_1$
$\vdots$
test$_T$

Here, test$_i$ represents the $i$-th test case, which is given in the following format.

$x \ y \ a$
  • All input values are integers.
  • $1 \le T \le 10^4$
  • $0 \le x, y \le 10^{18}$
  • $2 \le a \le 10^{18}$

Output

Output $T$ lines. On the $i$-th line, output the answer for the $i$-th test case.

Sample

Sample 1
Input
4
7 4 2
20 11 10
12093298 4893042 23
0 0 2
Output
2
4
41
0

In the first test case, the integer written on the blackboard can be made $4$ with $2$ operations as follows.

  1. Replace $7$ with $7 + 1 = 8$.
  2. Replace $8$ with $\lfloor \frac{8}{2} \rfloor = 4$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。