题解 P2696 【慈善的约瑟夫】
解题思路
看这个数据,模拟是打不过了,那我们考虑找规律。
对于这类问题,我们有递推式:
其中
我们发现,当
所以接下来的代码就很简单了。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
int n;
int f[N],ans;
int dfs(int x){
if(x==f[x]) return f[x];
dfs(f[x]);
}
int main(){
scanf("%d",&n);f[1]=1;
for(int i=1;i<=n;i++) f[i*2]=2*f[i]-1,f[i*2+1]=2*f[i]+1;
printf("%d",dfs(n)+n);
}