Codeforces1487 B. Cat Cycle (Mathematics)

Title:

Insert picture description here

solution:

The problem is to find the position of the kth second, that is, the position after k − 1 steps. When n is an even number, two cats will not meet. When n is an odd number: meet once every n 2 seconds, each time b cat The position of is increased by 1, then cat b increases by n 2 + 1 from the beginning to the meeting position. Set x = n / 2, then ans = (kx ∗ (x + 1) + k% x)% n + 1 The position of k seconds, that is, the position after k-1 steps. \\ When n is an even number, two cats will not meet. \\ When n is an odd number: \\ every \frac(n)(2) seconds Encounter once, each time you encounter, the position of cat b increases by 1, \\ Then cat b increases from the beginning to the position of the encounter \frac(n)(2)+1, \\ Set x=n/2, then ans=( \frac{k}{x}*(x+1)+k\%x)\%n+1 Topic entry request of k sec the position set ,That go kA step of after the bit is set .When n is an even number of the time ,Liang Zhi cats do not will phase encounter .When n is an odd number when:each2nSecond phase in case of a second ,Each sub- phase encounter ,b cat 's position set by adding 1 ,That it b cat from open start to phase in case of position set by adding a2n+1,Let x=n/2,Then a n s=(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;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/114119405