codeforces day 3 Cat Cycle

题目:

B. Cat Cycle

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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: 𝑛,𝑛−1,𝑛−2,…,3,2,1,𝑛,𝑛−1,…n,n−1,n−2,…,3,2,1,n,n−1,… In other words, at the first hour it's on the spot 𝑛nand then goes in decreasing order cyclically; 
  • Cat B changes its napping place in order: 1,2,3,…,𝑛−1,𝑛,1,2,…1,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 𝑥xthen the A takes this place and B moves to the next place in its order (if 𝑥<𝑛x<n then to 𝑥+1x+1, but if 𝑥=𝑛x=n then to 11). 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 𝑥+2x+2 and so on.

Calculate, where cat B will be at hour 𝑘k?

Input

The first line contains a single integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases.

The first and only line of each test case contains two integers 𝑛n and 𝑘k (2≤𝑛≤1092≤n≤109; 1≤𝑘≤1091≤k≤109) — the number of spots and hour 𝑘k.

Output

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

分析:

A猫休息的规律是n,n-1,n-2....3,2,1,n,n-1...3,2,1;

B猫休息的规律是1,2,3,..n-1,n,1,2,3..n-1,n

当A,B猫都在同一个地方休息的时候,A猫休息轨迹不受影响,B猫按着规律向下一个场所移动。当n&1=0的时候两只猫不会相遇,当n&1=1的时候两只猫会相遇。

当n&1=0的时候如果k<n那么B猫所在的位置就是k,当n&1=1的时候B猫所在的位置就是n%k;

当n&1=1的时候,B猫所在的位置和相遇的次数相关,发现他们每相遇一次则B会多前进一位。且B从第二个地方开始,每n/2次必定会与A相遇一次(也就是可以多前进一个单位)。则k确定的基本步数加上额外加成再取模,就是结果。

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k,a;
        cin>>n>>k;
        if(n%2==1)
        {
            int meet=(k-1)/(n/2);
            int basic_step=k-1;
            cout<<1+(basic_step+meet)%n;//先后顺序,思路
        }
                    else
                    {
                     
                        a=k%n==0?n:k%n;
                        cout<<a;
                        
                    }//替代问题
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值