简单约瑟夫环模板(C++版)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/westbrook1998/article/details/83549295

hdu2211

#include <bits/stdc++.h>
using namespace std;
int t;
long long n,k;
//函数返回的就是胜利者编号
long long cir(long long n,long long m){
    //递归边界
    if(n==k){
        return k;
    }
    //一轮过去后剩下n-n/k人
    //x就是下一轮也就是最后胜利者的编号
    long long x=cir(n-n/k,k);
    //在这一轮的编号t=(x-1)-(x-1)/k+1再化简
    return (x-1)/(k-1)+x;
}
int main(void){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&k);
        printf("%lld\n",cir(n,k));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/westbrook1998/article/details/83549295