题目大意
$YJC$很喜欢玩游戏,今天他决定和朋友们玩约瑟夫游戏。
约瑟夫游戏的规则是这样的:$n$个人围成一圈,从$1$号开始依次报数,当报到$m$时,报$1,2,\ldots,m-1$的人出局,下一个人接着从$1$开始报,保证$(n-1)$是$(m-1)$的倍数。最后剩的一个人获胜。
$YJC$很想赢得游戏,但他太笨了,他想让你帮他算出自己应该站在哪个位置上。
题目分析
这根本不是约瑟夫游戏啊!
我们尝试使用递归的过程将问题转化为其子问题。

根据题意,红色的点是会被保留下来的。
其中蓝框中的点不足$m$个,放在当前轮处理很麻烦,不如将其归到下一次的子问题中。
这样我们会选出$x/m+x\%m$个点,且对于下一轮来说,蓝框中的点不会成为答案(因为不足$m$个),若在下一轮得出答案,必定是非蓝框中的红点。
这样我们需要减去$x\%m$个蓝框中的点再乘上$m$才能对应到原有编号。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } LL n,m; LL Solve(LL x) { if(x==1)return 1; return m*(Solve(x/m+x%m)-x%m); } int main() { n=Get_Int(); m=Get_Int(); printf("%lld\n",Solve(n)); return 0; }
|