现在有 n 个人, 编号从 0 到 n−1 站成一圈, 编号为i 的人右手边是编号为 i+1 的人(n−1 的右手边为 0). 进行一种约瑟夫环游戏的变种.
游戏一共进行 n−1 轮, 一开始编号为 0 的人手里拿着旗子.
在第 i 轮(从 1 开始数), 手上拿着旗子的人把旗子传给他右手边的人, 一共传 w[n-i] 次, 传完之后拿着旗子的人退出队伍, 并把旗子传给他右手边的人.
问最后剩在队伍里的人是谁.
比如 n=3, 粗体数字代表拥有旗子, w[2]=2, w[1]=1.
第一轮开始时状态是 0, 1, 2
旗子传 2 次之后的状态是 0, 1, 2
然后 2 被踢出队伍, 把旗子传给 0.
第二轮开始时状态是 0, 1
传 1 次之后的状态是 0, 1
然后 1 被踢出.
剩下的人编号为 0.
输入格式
先读入一个 n≤10^6
接下来 n−1 个整数, 分别代表 w[n−1],w[n−2],⋯,w[2],w[1], 1≤w[i]≤10^9.
样例输入
3
1 2
样例输出
0
First, I would like to thank my TA Hao Ling, who has just got 13-th position in ICPC World Final for NJU with other two teammates and one teacher. This problem was provided by him and another TA Ziyang Li.
Now I'd like to briefly talk about this problem, which is actually very simple.
Define F[i]: When there are altogether i people, F[i] stores the serial number of the last person left. and we have F[1]=0, and F[i]=(w[i-1]%i+1+f[i-1])%i.
When we have i people, we can know who is going to be the last person given the information stored in F[i-1]. When we have i people, first we have to start from the person with serial number. And the count till temp=w[i-1]%i(Why i-1?). Person with serial number temp will get out and next time we shall start from (temp+1)%i(Why %i?). And now we jump into a situation quite similar when we were dealling with i-1 people before we deal with i people. The only difference is that the first person who starts the game is different and some of their serial number is changed. But it does not matter. All we need to know is that from person (temp+1)%i, we know that person (temp+1+F[i-1])%i is to get out, considering the situation when there are i-1 people.
本文介绍了一个约瑟夫环游戏的变种问题,通过递推公式F[i]=(w[i-1]%i+1+F[i-1])%i找到最后剩余的人的编号。详细解释了递推公式的由来及每一步操作的意义。
811

被折叠的 条评论
为什么被折叠?



