CF1487B.Cat Cycle

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Suppose you are living with two cats: A and B. There are nn napping spots where both cats usually sleep.

Your cats like to sleep and also like all these spots, so they change napping spot each hour cyclically:

  • Cat A changes its napping place in order: n,n−1,n−2,…,3,2,1,n,n−1,…n, n - 1, n - 2, \dots, 3, 2, 1, n, n - 1, \dots In other words, at the first hour it's on the spot nn and then goes in decreasing order cyclically;
  • Cat B changes its napping place in order: 1,2,3,…,n−1,n,1,2,…1, 2, 3, \dots, n - 1, n, 1, 2, \dots In other words, at the first hour it's on the spot 11 and then goes in increasing order cyclically.

The cat B is much younger, so they have a strict hierarchy: A and B don't lie together. In other words, if both cats'd like to go in spot xx then the A takes this place and B moves to the next place in its order (if x<nx \lt n then to x+1x + 1, but if x=nx = n then to 11). Cat B follows his order, so it won't return to the skipped spot xx after A frees it, but will move to the spot x+2x + 2 and so on.

Calculate, where cat B will be at hour kk?

假设你和两只猫 A 和 B 一起生活。一共有 nn 个供它们午睡的位置,两只猫通常都在这些位置上睡觉。

你的猫喜欢睡觉,也喜欢所有这些位置,因此它们每小时都会按循环顺序更换午睡位置:

  • 猫 A 的午睡位置变化顺序为:n,n−1,n−2,…,3,2,1,n,n−1,…n, n - 1, n - 2, \dots, 3, 2, 1, n, n - 1, \dots;换言之,第 1 小时它在位置 nn,之后按递减顺序循环;
  • 猫 B 的午睡位置变化顺序为:1,2,3,…,n−1,n,1,2,…1, 2, 3, \dots, n - 1, n, 1, 2, \dots;换言之,第 1 小时它在位置 11,之后按递增顺序循环。

猫 B 年龄更小,因此存在严格的等级关系:A 和 B 不会同时躺在同一个位置上。换句话说,若两只猫都想前往位置 xx,则猫 A 占据该位置,而猫 B 则跳转至其序列中的下一个位置(若 x<nx \lt n,则前往 x+1x + 1;若 x=nx = n,则前往 11)。猫 B 始终遵循其既定顺序,因此当猫 A 离开位置 xx 后,猫 B 不会返回该已被跳过的位置 xx,而是继续前进至位置 x+2x + 2,依此类推。

请计算:在第 kk 小时时,猫 B 所在的位置是哪里?

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first and only line of each test case contains two integers nn and kk (2≤n≤1092 \le n \le 10^9; 1≤k≤1091 \le k \le 10^9) — the number of spots and hour kk.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例仅有一行,包含两个整数 nn 和 kk(2≤n≤1092 \le n \le 10^9;1≤k≤1091 \le k \le 10^9)—— 分别表示斑点数量和小时数 kk。

输出格式

For each test case, print one integer — the index of the spot where cat B will sleep at hour kk.

对于每个测试用例,输出一个整数——表示猫 B 在第 kk 小时睡觉的位置索引。

输入输出样例

  • 输入#1

    7
    2 1
    2 2
    3 1
    3 2
    3 3
    5 5
    69 1337

    输出#1

    1
    2
    1
    3
    2
    2
    65

说明/提示

In the first and second test cases n=2n = 2, so:

  • at the 11-st hour, A is on spot 22 and B is on 11;
  • at the 22-nd hour, A moves to spot 11 and B — to 22.

If n=3n = 3 then:

  • at the 11-st hour, A is on spot 33 and B is on 11;
  • at the 22-nd hour, A moves to spot 22; B'd like to move from 11 to 22, but this spot is occupied, so it moves to 33;
  • at the 33-rd hour, A moves to spot 11; B also would like to move from 33 to 11, but this spot is occupied, so it moves to 22.

In the sixth test case:

  • A's spots at each hour are [5,4,3,2,1][5, 4, 3, 2, 1];
  • B's spots at each hour are [1,2,4,5,2][1, 2, 4, 5, 2].

在第一和第二个测试用例中,n=2n = 2,因此:

  • 在第 11 小时,A 位于位置 22,B 位于位置 11;
  • 在第 22 小时,A 移动到位置 11,B 移动到位置 22。

若 n=3n = 3,则:

  • 在第 11 小时,A 位于位置 33,B 位于位置 11;
  • 在第 22 小时,A 移动到位置 22;B 想从位置 11 移动到位置 22,但该位置已被占据,因此 B 改为移动到位置 33;
  • 在第 33 小时,A 移动到位置 11;B 同样想从位置 33 移动到位置 11,但该位置已被占据,因此 B 改为移动到位置 22。

在第六个测试用例中:

  • A 在每个小时的位置序列为 [5,4,3,2,1][5, 4, 3, 2, 1];
  • B 在每个小时的位置序列为 [1,2,4,5,2][1, 2, 4, 5, 2]。

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

首页