CF1487B Cat Cycle

题目描述

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, \dots, 3, 2, 1, n, n - 1, \dotsn,n−1,n−2,…,3,2,1,n,n−1,… 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, \dots, n - 1, n, 1, 2, \dots1,2,3,…,n−1,n,1,2,… 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<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 ?

输入格式

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

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

输出格式

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

题意翻译

假设有两只猫:猫 A 和猫 B ,共同拥有 nn 个休息地点排成一个环,位置顺时针编号为 11 至 nn,他们要选择休息位置。

对于选择休息位置,两只猫有不同的策略:

猫 A 在第 11 小时会选择休息位置 nn,接下来每过一小时它就会逆时针移一个位置,也就是说它选择的位置的序列为 n,n-1,n-2,...,3,2,1,n,n-1,...n,n−1,n−2,...,3,2,1,n,n−1,...。

猫 B 在第 11 小时会选择休息位置 11,接下来每过一小时它就会顺时针移一个位置,也就是说它选择的位置的序列为 1,2,3,...,n-1,n,1,2,...1,2,3,...,n−1,n,1,2,...。

特别的,因为猫 A 比猫 B 老,所以猫 A 比猫 B 地位高一些,因此当两只猫相中了同一个位置时,由猫 A 占领这个位置,猫 B 会再顺时针移一个位置到下一个位置。

给定 n,kn,k,求出第 kk 小时猫 B 在哪个位置。

TT 组询问。

1\leq T\leq10^4;2\leq n\leq10^9;1\leq k\leq10^9;1≤T≤104;2≤n≤109;1≤k≤109;

输入输出样例

输入 #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] .

题目类型: 推理题;

解题目标:B最后所在的位置;

解题思路:1)B和A遇到时, B要让A先过, 自己呆在原位;

                  2)n为偶数时, 永远不会冲突; (k-1)%n+1; n 为偶数 ==  k % n  ;(k 从1数起)

                 3)n为奇数时,移动次数  (k - k/((n-1)/2))mod n == (k/((n-1)>>2) * ((n+1)/2) + k%((n-1)>>2))%n +1;

               AC代码:

#include <bits/stdc++.h>
#define rep(x ,a, b) for(int x =a; x<=b; x++)
#define inf 0x3f3f3f3f
using namespace std;
const int N= 1e3+10;

int main()
{
	int c;
	cin>>c;
	while(c--)
	{
		int n, k;
		cin>>n>>k;
		k--;
		if(n % 2 == 0) 
		{
			cout<<(k%n)+1<<endl;
		}
		else 
		{
			int t = (n-1)>>1 ;
			cout<<(k/t+k)%n + 1<<endl;
		}
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/m0_54674275/article/details/121432306
cat
今日推荐