Number Discovery

시간 제한2초메모리 제한1024 MB

요약
쓰이지 않은 가장 작은 k개의 수와 그 합을 계속 이어 붙여 만든 무한 수열에서 n의 위치를 구한다.
난이도

보통10점 중 7점

유형
수학, 구현
정답자
아직 제출이 없습니다

문제

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 u_1,u_2,…,u_ku\_{1}, u\_{2}, \ldots, u\_{k} from the smallest to the largest.
  2. Append u_1,u_2,…,u_ku\_{1}, u\_{2}, \ldots, u\_{k} and ∑_i=1ku_i\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 s_x=ns\_{x} = n. It's possible to prove that all positive integers are included in ss only once.

입력

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.

출력

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

힌트

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).

예제1

  1. 예제 1

    입력
    2
    10 2
    40 5
    
    예상 출력
    11
    12