改编过的约瑟夫问题。不同的是每次出局的编号是变化的。直接暴力模拟依据美观程度 \(O(n^2)\) 到 \(O(n\log n)\) 不等。
那么考虑正解。我们现在知道的是最后剩下的人是谁,求最初的编号,是逆推。我们可以将所有编号 \(-1\),第 \(i\) 轮就是上次游戏后重新编号后,选择第 \(i-1\) 号人出局。
我们只需要考虑如何从这一轮推到上一轮的编号。用 \(x\) 表示最后剩下的人在当前编号状态下的编号,那么递推式子就是(\(x\) +当前状态下要出局的人的编号 (\(n-i+1\))) \(\text{mod}\) 上一状态剩下的人的数量(\(i\))。注意是逆推的,所以是当前要出局的人是 \(n-i+1\)。
#include <bits/stdc++.h>
using namespace std;
const int maxn=3e3+10;
int n;
inline int read(){
int x=0;bool fopt=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)fopt=0;
for(;isdigit(ch);ch=getchar())x=(x<<3)+(x<<1)+ch-48;
return fopt?x:-x;
}
int main(){
#ifndef LOCAL
freopen("one.in","r",stdin);
freopen("one.out","w",stdout);
#endif
int T=read();
while(T--){
n=read();
int x=0;
for(int i=2;i<=n;i++)
x=(x+n-i+1)%i;
printf("%d\n",x+1);
}
return 0;
}
邹队太巨辣!%%%
原文:https://www.cnblogs.com/Midoria7/p/13781220.html