CF1988A.Split the Multiset

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Split the Multiset

A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, {2,2,4}\{2,2,4\} is a multiset.

You have a multiset SS. Initially, the multiset contains only one positive integer nn. That is, S={n}S=\{n\}. Additionally, there is a given positive integer kk.

In one operation, you can select any positive integer uu in SS and remove one copy of uu from SS. Then, insert no more than kk positive integers into SS so that the sum of all inserted integers is equal to uu.

Find the minimum number of operations to make SS contain nn ones.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). Description of the test cases follows.

The only line of each testcase contains two integers n,kn,k (1n1000,2k10001\le n\le 1000,2\le k\le 1000).

Output

For each testcase, print one integer, which is the required answer.

Note

For the first test case, initially S={1}S=\{1\}, already satisfying the requirement. Therefore, we need zero operations.

For the second test case, initially S={5}S=\{5\}. We can apply the following operations:

  • Select u=5u=5, remove uu from SS, and insert 2,32,3 into SS. Now, S={2,3}S=\{2,3\}.
  • Select u=2u=2, remove uu from SS, and insert 1,11,1 into SS. Now, S={1,1,3}S=\{1,1,3\}.
  • Select u=3u=3, remove uu from SS, and insert 1,21,2 into SS. Now, S={1,1,1,2}S=\{1,1,1,2\}.
  • Select u=2u=2, remove uu from SS, and insert 1,11,1 into SS. Now, S={1,1,1,1,1}S=\{1,1,1,1,1\}.

Using 44 operations in total, we achieve the goal.

For the third test case, initially S={6}S=\{6\}. We can apply the following operations:

  • Select u=6u=6, remove uu from SS, and insert 1,2,31,2,3 into SS. Now, S={1,2,3}S=\{1,2,3\}.
  • Select u=2u=2, remove uu from SS, and insert 1,11,1 into SS. Now, S={1,1,1,3}S=\{1,1,1,3\}.
  • Select u=3u=3, remove uu from SS, and insert 1,1,11,1,1 into SS. Now, S={1,1,1,1,1,1}S=\{1,1,1,1,1,1\}.

Using 33 operations in total, we achieve the goal.

For the fourth test case, initially S={16}S=\{16\}. We can apply the following operations:

  • Select u=16u=16, remove uu from SS, and insert 4,4,4,44,4,4,4 into SS. Now, S={4,4,4,4}S=\{4,4,4,4\}.
  • Repeat for 44 times: select u=4u=4, remove uu from SS, and insert 1,1,1,11,1,1,1 into SS.

Using 55 operations in total, we achieve the goal.

Samples

4
1 5
5 2
6 3
16 4
0
4
3
5

在线编程 IDE

建议全屏模式获得最佳体验