Score : 500 points
Problem Statement
There is an array A1,…,AN, and initially Ai=i for all i. Define the following routine shuffle(L,R):
- If R=L+1, swap values of AL and AR and terminate.
- Otherwise, run shuffle(L,R−1) followed by shuffle(L+1,R).
Suppose we run shuffle(1,N). Print the value of AK after the routine finishes.
For each input file, solve T test cases.
Constraints
- 1≤T≤1000
- 2≤N≤1018
- 1≤K≤N
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each case is in the following format:
N K
Output
Print T lines. The i-th line should contain the answer for the i-th case.
7
2 1
2 2
5 1
5 2
5 3
5 4
5 5
2
1
2
4
1
5
3
For N=2, we do the following and get A=(2,1).
- Run shuffle(1,2), and swap A1 and A2.
For N=5, we do the following and get A=(2,4,1,5,3).
- Run shuffle(1,5):- Run shuffle(1,4):- Run shuffle(1,3):- ⋮
- Run shuffle(2,4):- ⋮
- Run shuffle(2,5):- Run shuffle(2,4):- ⋮
- Run shuffle(3,5):- ⋮
- Run shuffle(1,4):
- Run shuffle(1,3):
- ⋮
- Run shuffle(2,4):
- ⋮
- Run shuffle(2,5):
- Run shuffle(2,4):
- ⋮
- Run shuffle(3,5):
- ⋮