AT_abc465_d.X to Y

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given integers X,YX,Y, and an integer KK that is at least 22.

There is a variable xx, which is initially x=Xx=X. You can perform the following operation on xx zero or more times:

  • Choose an integer yy satisfying ⌊xK⌋=y\displaystyle \left\lfloor \frac xK \right\rfloor=y or ⌊yK⌋=x\displaystyle \left\lfloor \frac yK \right\rfloor=x, and replace the value of xx with yy.

Here, ⌊z⌋\displaystyle \left\lfloor z \right\rfloor is defined as the greatest integer not exceeding zz, for a real number zz.

Find the minimum number of operations required to make x=Yx=Y. Under the given constraints, it can be proved that there always exists a way to make x=Yx=Y in a finite number of operations.

You are given TT test cases; solve each of them.

给你整数 XX、YY 和一个不小于 22 的整数 KK。

有一个变量 xx,其初始值为 x=Xx = X。你可以对 xx 执行以下操作零次或多次:

  • 选择一个整数 yy,满足 ⌊xK⌋=y\displaystyle \left\lfloor \frac{x}{K} \right\rfloor = y 或 ⌊yK⌋=x\displaystyle \left\lfloor \frac{y}{K} \right\rfloor = x,然后将 xx 的值替换为 yy。

其中,对任意实数 zz,⌊z⌋\displaystyle \left\lfloor z \right\rfloor 表示不超过 zz 的最大整数。

求使 x=Yx = Y 所需的最少操作次数。在给定约束条件下,可以证明总存在一种方式,在有限步操作内使 x=Yx = Y。

你将得到 TT 组测试用例,请分别求解每组。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

The ii-th (1≤i≤T)(1\le i\le T) test case casei\text{case}_i is given in the following format:

XX YY KK

输入从标准输入中以如下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

第 ii 个(1≤i≤T1\le i\le T)测试用例 casei\text{case}_i 以如下格式给出:

XX YY KK

输出格式

Output the answers for the test cases in order, separated by newlines.

按顺序输出测试用例的答案,每个答案占一行。

输入输出样例

  • 输入#1

    4
    11 9 3
    0 0 2
    842 180 7
    1948706013487601 48019760148910476 89014537

    输出#1

    2
    0
    7
    5

说明/提示

Sample 1 Explanation:
Consider the first test case.

By performing the following operations, x=Yx=Y can be achieved in two operations:

  • Choose y=3y=3. Since ⌊xK⌋=⌊113⌋=3\displaystyle\left\lfloor\frac{x}K \right\rfloor=\left\lfloor\frac{11}3 \right\rfloor=3, this choice is valid. Then, replace the value of xx with 33.
  • Choose y=9y=9. Since ⌊yK⌋=⌊93⌋=3\displaystyle\left\lfloor\frac{y}K \right\rfloor=\left\lfloor\frac{9}3 \right\rfloor=3, this choice is valid. Then, replace the value of xx with 99.

x=Yx=Y cannot be achieved in fewer than two operations, so output 22 on the first line.

Constraints

  • 1≤T≤2×1051\le T\le 2\times 10^5
  • 0≤X,Y≤10180\le X,Y\le 10^{18}
  • 2≤K≤10182\le K\le 10^{18}
  • All input values are integers.

样例 1 解释:
考虑第一个测试用例。

通过执行以下操作,可以在两次操作内使 x=Yx=Y 成立:

  • 选择 y=3y=3。由于 ⌊xK⌋=⌊113⌋=3\displaystyle\left\lfloor\frac{x}K \right\rfloor=\left\lfloor\frac{11}3 \right\rfloor=3,该选择合法。然后将 xx 的值替换为 33。
  • 选择 y=9y=9。由于 ⌊yK⌋=⌊93⌋=3\displaystyle\left\lfloor\frac{y}K \right\rfloor=\left\lfloor\frac{9}3 \right\rfloor=3,该选择合法。然后将 xx 的值替换为 99。

无法在少于两次操作内使 x=Yx=Y 成立,因此第一行输出 22。

约束条件

  • 1≤T≤2×1051\le T\le 2\times 10^5
  • 0≤X,Y≤10180\le X,Y\le 10^{18}
  • 2≤K≤10182\le K\le 10^{18}
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页