6835.Josephus Queries

Time Limit: 1s Memory Limit: 512MB

Consider a game where there are $n$ children (numbered $1,2,\dots,n$ ) in a circle. During the game, every second child is removed from the circle, until there are no children left.

Your task is to process $q$ queries of the form: "when there are $n$ children, who is the $k$ th child that will be removed?"

Input Format(From the terminal/stdin)

The first input line has an integer $q$ : the number of queries.

After this, there are $q$ lines that describe the queries. Each line has two integers $n$ and $k$ : the number of children and the position of the child.

  • $1 \le q \le 10^5$
  • $1 \le k \le n \le 10^9$

Output Format(To the terminal/stdout)

Print $q$ integers: the answer for each query.

Sample Input

Copy
4
7 1
7 3
2 2
1337 1313
 \n
 · \n
 · \n
 · \n
    ·    \n

Sample Output

Copy
2
6
1
1107
 \n
 \n
 \n
    \n
Source: CSES, Mathematics, 2164

Submit

请先 登录

© 2025 FAQs