题目来源

一、题目

Suppose you are living with two cats: A and B. There are n 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,… In other words, at the first hour it’s on the spot n and then goes in decreasing order cyclically;
Cat B changes its napping place in order: 1,2,3,…,n−1,n,1,2,… In other words, at the first hour it’s on the spot 1 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 x then the A takes this place and B moves to the next place in its order (if x<n then to x+1, but if x=n then to 1). Cat B follows his order, so it won’t return to the skipped spot x after A frees it, but will move to the spot x+2 and so on.
Calculate, where cat B will be at hour k?

两只小猫A和B,有n个窝,A猫按n,n-1,…2,1,n,n-1,n-2…的顺序挪窝,B猫按1,2…n-1,n,1,2,…,n的顺序挪窝,每小时挪一次。假如两猫相遇,B猫会挪到下一个位置。问:在第k小时B猫在哪?

二、输入

The first line contains a single integer t (1≤t≤1e4) — the number of test cases.
The first and only line of each test case contains two integers n and k (2≤n≤1e9; 1≤k≤1e9) — the number of spots and hour k.

第一行测试样例数t
第二行窝数n和小时k

三、输出

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

输出B在第k小时所在的窝的编号

四、样例

input:

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

output:

1
2
1
3
2
2
65

Note
In the first and second test cases n=2, so:
at the 1-st hour, A is on spot 2 and B is on 1;
at the 2-nd hour, A moves to spot 1 and B — to 2.
If n=3 then:
at the 1-st hour, A is on spot 3 and B is on 1;
at the 2-nd hour, A moves to spot 2; B’d like to move from 1 to 2, but this spot is occupied, so it moves to 3;
at the 3-rd hour, A moves to spot 1; B also would like to move from 3 to 1, but this spot is occupied, so it moves to 2.
In the sixth test case:
A’s spots at each hour are [5,4,3,2,1];
B’s spots at each hour are [1,2,4,5,2].

五、思路 + 代码

一道简单题,很容易想到用模拟的方法去解,但是1e9的范围用o(n)复杂度的方法的话极大可能就超时了,还是要找数学规律。
n分两种情况,奇数和偶数。偶数时,两只猫不会相遇;奇数时,每走n/2步相遇一次。分类讨论即可。

代码如下:

#include<iostream>
using namespace std;
int n, k, ans;
void solve() {cin >> n >> k;if (n & 1) {int meet = (k - 1) / (n >> 1);int step = k - 1;ans = 1 + (step + meet) % n;}else {ans = k % n == 0 ? n : k % n;}cout << ans << endl;
}
int main() {int t;cin >> t;while (t--) {solve();}return 0;
}

B. Cat Cycle相关推荐

  1. Educational Codeforces Round 104 (Rated for Div. 2)A~E解题报告

    Educational Codeforces Round 104 (Rated for Div. 2) A. Arena \quad原题链接 http://codeforces.com/contest ...

  2. 【ACM】2022.7.31训练赛

    博客:https://www.cnblogs.com/heystar/ A.Arena CodeForces - 1487A 题目描述 $ n $ heroes fight against each ...

  3. SDWU 2021 Autumn Training Series C1 1st Round题解

    A Blend of Springtime [题目大意]给定一个字符串,问是否存在3个相邻位置,使字母'A','B','C'各出现1次,是的话输出Yes,否则输出No [思路]for一遍,看看是否存在 ...

  4. Educational Codeforces Round 104 (Rated for Div. 2) A,B,C,D,E

    Educational Codeforces Round 104 (Rated for Div. 2) A,B,C,D,E A - Arena 题意 nnn 个英雄,他们的等级分别是 a1,a2,-, ...

  5. Educational Codeforces Round 104 (Rated for Div. 2)

    A. Arena 题目传送门: A. Arena 题目大意: 有n个人,每个人都有战斗力,当两个人打起来时,战斗力高的人赢并且战斗力加1,当战斗力达到100500时,则成为英雄.问能有多少人能成为英雄 ...

  6. svn tour cycle

    svn tour cycle https://i18n-zh.googlecode.com/svn/www/svnbook-1.4/svn.tour.cycle.html 基本的工作循环 上一页  第 ...

  7. Smarty - 手册 - 第8章 自定义函数 - {cycle}循环

    Smarty - 手册 - 第8章 自定义函数 - {cycle}循环 {cycle} {cycle} is used to alternate a set of values. This makes ...

  8. Ubuntu中的Load/Unload Cycle Count问题及解决方案

    说明:请大家注意,本文所描述的问题只有笔记本电脑才会出现. 1. 问题描述 几周前收到soldiers童鞋的短信说,Ubuntu伤硬盘?我说没事,好多人都用呢.过了一周,soldiers童鞋又问,Ub ...

  9. Load Cycle Count

    Load/Unload Cycle Count 此BUG早就被修复了. ubuntu对于这个问题的说明 https://wiki.ubuntu.com/DanielHahler/Bug59695#he ...

最新文章

  1. schema类SpringMVC+Hibernate+Spring整合(二)
  2. vue2.0中Ajax库(axios)
  3. [转]Emacs 系列教程
  4. mysql切换用户sql语句,MySQL用户管理及SQL语句详解
  5. java实现验证码图片_java实现验证码图片
  6. adb-获取包名/界面名、获取app启动时间、卸载app、退出app、查看所有进程
  7. linux命令进入用户模式,linux怎么切换到root用户模式
  8. 树莓派python编程小车_树莓派小车教程(三)——软件代码
  9. 45岁以后的IT人生
  10. 基于时间片的高优先级调度_CFS调度器(1)-基本原理
  11. C语言bmp转JPEG不用库函数,C++图片格式转换:BMP转JPEG
  12. WannaRen勒索软件解密密码计算工具发布
  13. 【原】制造垃圾邮件设想【未尝试】
  14. Druid监控页面配置与使用
  15. 电脑使用技巧 快捷键
  16. 权威大数据应用解决方案
  17. 高中生参加的计算机奥赛是,高中生可参加哪些含金量较高的赛事?报名流程指南来了!...
  18. 根据身高体重计算某个人的BMI值
  19. 缓冲协议才是Python风靡全球的主要原因
  20. 零基础如何自学编程?| 程序员有话说

热门文章

  1. 1、misa统计SRR结果
  2. arduino安装+esp32+esp8266安装
  3. 手绘机器学习全流程,教你如何实现模型训练
  4. 华为交换机导入配置_华为交换机怎么配置FTP上传文件?
  5. 艺赛旗(RPA)国家企业信用信息公示系统验证码破解(一)
  6. 中国平安产险总经理易人 吴鹏已获保监会核准
  7. 模拟器之AVD模拟器
  8. 使用RDS for MySQL数据库(本地连接、数据库迁移、JDBC操作)
  9. 一般熟练盲打需要多久_学会盲打要多长时间,每天要练多长时间 盲打要练多久...
  10. Python获取列表list中的非零数据、第一个非零元素、最后一个非零元素