CF1242D.Number Discovery

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers nn and kk. He creates an infinite sequence ss by repeating the following steps.

  1. Find kk smallest distinct positive integers that are not in ss. Let's call them u1,u2,…,uku_{1}, u_{2}, \ldots, u_{k} from the smallest to the largest.
  2. Append u1,u2,…,uku_{1}, u_{2}, \ldots, u_{k} and ∑i=1kui\sum_{i=1}^{k} u_{i} to ss in this order.
  3. Go back to the first step.

Ujan will stop procrastinating when he writes the number nn in the sequence ss. Help him find the index of nn in ss. In other words, find the integer xx such that sx=ns_{x} = n. It's possible to prove that all positive integers are included in ss only once.

乌詹在打扫卫生后需要休息,于是开始玩起了无穷序列。他有两个整数 nn 和 kk。他通过重复以下步骤来构造一个无穷序列 ss:

  1. 找出尚未出现在 ss 中的 kk 个最小的互异正整数,记为 u1,u2,…,uku_{1}, u_{2}, \ldots, u_{k}(按从小到大的顺序)。
  2. 将 u1,u2,…,uku_{1}, u_{2}, \ldots, u_{k} 及其和 ∑i=1kui\sum_{i=1}^{k} u_{i} 按此顺序追加到 ss 的末尾。
  3. 返回第 1 步继续执行。

乌詹将在序列 ss 中首次写出数字 nn 时停止拖延。请帮助他找出 nn 在 ss 中的下标,即求满足 sx=ns_{x} = n 的整数 xx。可以证明:所有正整数在 ss 中恰好出现一次。

输入格式

The first line contains a single integer tt (1≤t≤1051 \le t \le 10^{5}), the number of test cases.

Each of the following tt lines contains two integers nn and kk (1≤n≤10181 \le n \le 10^{18}, 2≤k≤1062 \le k \le 10^{6}), the number to be found in the sequence ss and the parameter used to create the sequence ss.

第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^{5}),表示测试用例的数量。

接下来的 tt 行中,每行包含两个整数 nn 和 kk(1≤n≤10181 \le n \le 10^{18},2≤k≤1062 \le k \le 10^{6}),分别表示要在序列 ss 中查找的数以及用于构造序列 ss 的参数。

输出格式

In each of the tt lines, output the answer for the corresponding test case.

在每行的 tt 行中,输出对应测试用例的答案。

输入输出样例

  • 输入#1

    2
    10 2
    40 5

    输出#1

    11
    12

说明/提示

In the first sample, s=(1,2,3,4,5,9,6,7,13,8,10,18,…)s = (1, 2, 3, 4, 5, 9, 6, 7, 13, 8, 10, 18, \ldots). 1010 is the 1111-th number here, so the answer is 1111.

In the second sample, s=(1,2,3,4,5,15,6,7,8,9,10,40,…)s = (1, 2, 3, 4, 5, 15, 6, 7, 8, 9, 10, 40, \ldots).

在第一个样例中,s=(1,2,3,4,5,9,6,7,13,8,10,18,…)s = (1, 2, 3, 4, 5, 9, 6, 7, 13, 8, 10, 18, \ldots)。1010 是该序列中的第 1111 个数,因此答案为 1111。

在第二个样例中,s=(1,2,3,4,5,15,6,7,8,9,10,40,…)s = (1, 2, 3, 4, 5, 15, 6, 7, 8, 9, 10, 40, \ldots)。

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

首页