HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Algorithms and Data Structures > problem:


25. Josephus Queries

Volume problems

• 43. Finding Periods
• 44. Minimal Rotation
• 45. Static Range Sum Queries
• 01. Автобус в Джал
• 03. Новый язык программирования
• 05. Trailing Zeros
• 17. Number Spiral
• 23. Apple Division
• 25. Josephus Queries
• 26. Exponentiation
• 29. Coin Combinations I

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb. Difficulty Beta

Consider a game where there are n children (numbered 1,2,…,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
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.

Output
Print q integers: the answer for each query.
Constraints
1≤q≤105
1≤k≤n≤109
Example

input:
4
7 1
7 3
2 2
1337 1313
output:
2
6
1
1107

Для отправки решений необходимо выполнить вход.

www.contester.ru