Edge Elimination

AtCoder
IDabc290_g
Time2000ms
Memory256MB
Difficulty
Solve the following problem for $T$ test cases. We have a perfect $K$\-ary tree of depth $D$ (with $1+K+K^2+\dots+K^D$ vertices). Your objective is to cut some of the edges to obtain a connected component with exactly $X$ vertices. At least how many edges must be cut to achieve the objective? ## Constraints * All values in the input are integers. * $1 \le T \le 100$ * $1 \le D$ * $2 \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: $T$ $case_1$ $\vdots$ $case_T$ Here, $case_i$ denotes the $i$\-th test case. Each test case is given in the following format: $D$ $K$ $X$ [samples]
Samples
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
Output #1
1
2
1
1
2
1
0
1
999999999999999998
1
0
API Response (JSON)
{
  "problem": {
    "name": "Edge Elimination",
    "description": {
      "content": "Solve the following problem for $T$ test cases. We have a perfect $K$\\-ary tree of depth $D$ (with $1+K+K^2+\\dots+K^D$ vertices).   Your objective is to cut some of the edges to obtain a connected com",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc290_g"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Solve the following problem for $T$ test cases.\nWe have a perfect $K$\\-ary tree of depth $D$ (with $1+K+K^2+\\dots+K^D$ vertices).  \nYour objective is to cut some of the edges to obtain a connected com...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments