#abc290g. G - Edge Elimination

G - Edge Elimination

Score : 600600 points

问题描述

对于 TT 个测试用例,解决以下问题。

我们拥有一棵深度为 DD 的完美 KK-叉树(包含 1+K+K2++KD1+K+K^2+\dots+K^D 个顶点)。
你的目标是切断一些边,以得到恰好包含 XX 个顶点的连通分量。
至少需要切断多少条边才能达到目标?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Solve the following problem for TT test cases.

We have a perfect KK-ary tree of depth DD (with 1+K+K2++KD1+K+K^2+\dots+K^D vertices).
Your objective is to cut some of the edges to obtain a connected component with exactly XX vertices.
At least how many edges must be cut to achieve the objective?

Constraints

  • All values in the input are integers.
  • 1T1001 \le T \le 100
  • 1D1 \le D
  • 2K2 \le K
  • $\displaystyle 1 \le X \le \sum_{i=0}^{D} K^i \le 10^{18}$

Input

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

TT

case1case_1

\vdots

caseTcase_T

Here, caseicase_i denotes the ii-th test case.

Each test case is given in the following format:

DD KK XX

Output

Print TT lines.
The ii-th line should contain the answer to the ii-th test case as an integer.

Sample Input 1

11
2 2 1
2 2 2
2 2 3
2 2 4
2 2 5
2 2 6
2 2 7
1 999999999999999999 1
1 999999999999999999 2
1 999999999999999999 999999999999999999
1 999999999999999999 1000000000000000000

Sample Output 1

1
2
1
1
2
1
0
1
999999999999999998
1
0

update @ 2024/3/10 12:07:40