Codeforces1487 B. Cat Cycle(数学)

题意:

在这里插入图片描述

解法:

题 目 求 第 k 秒 的 位 置 , 即 走 k − 1 步 之 后 的 位 置 . 当 n 为 偶 数 时 , 两 只 猫 不 会 相 遇 . 当 n 为 奇 数 时 : 每 n 2 秒 相 遇 一 次 , 每 次 相 遇 , b 猫 的 位 置 增 加 1 , 那 么 b 猫 从 开 始 到 相 遇 位 置 增 加 了 n 2 + 1 , 设 x = n / 2 , 则 a n s = ( k x ∗ ( x + 1 ) + k % x ) % n + 1 题目求第k秒的位置,即走k-1步之后的位置.\\ 当n为偶数时,两只猫不会相遇.\\ 当n为奇数时:\\ 每\frac{n}{2}秒相遇一次,每次相遇,b猫的位置增加1,\\ 那么b猫从开始到相遇位置增加了\frac{n}{2}+1,\\ 设x=n/2,则ans=(\frac{k}{x}*(x+1)+k\%x)\%n+1 k,k1.n,.n:2n,,b1,b2n+1,x=n/2,ans=(xk(x+1)+k%x)%n+1

code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
void solve(){
    
    
    cin>>n>>k;
    k--;
    if(n%2==0){
    
    
        cout<<k%n+1<<endl;
    }else{
    
    
        int x=n/2;
        int ans=(k/x*(x+1)+k%x)%n+1;
        cout<<ans<<endl;
    }
}
signed main(){
    
    
    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
    
    
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44178736/article/details/114119405