problem
People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.
The problem ( given the number(n) of people and the number(m) to be skipped ) is to choose the position in the initial circle to avoid execution.
solution
Now, we assume that the survival is the number
f
(
n
,
m
)
f(n,m)
f(n,m). And the array of people is
0
,
1
,
2
,
3
,
4
,
.
.
.
n
−
1
0, 1, 2, 3, 4, ... n-1
0,1,2,3,4,...n−1. So the
m
−
1
m-1
m−1 die at first. Then we mark the people anew:
m
→
0
,
m
+
1
→
1
,
.
.
.
.
.
.
,
n
−
1
→
n
−
m
−
1
,
0
→
n
−
m
,
.
.
.
.
.
.
,
m
−
2
→
n
−
2
m \rightarrow 0,m+1\rightarrow 1,......,n-1\rightarrow n-m-1,0\rightarrow n-m,......,m-2\rightarrow n-2
m→0,m+1→1,......,n−1→n−m−1,0→n−m,......,m−2→n−2.
Because survival’s position doesn’t change, its sequence number changes to
(
f
(
n
,
m
)
−
m
)
%
n
(f(n, m)-m)\%n
(f(n,m)−m)%n. And we will count from the next of the victim (now its sequence number is
0
0
0), So the winner will be the position as before.Therefore the survival’s sequence number of
n
−
1
n-1
n−1 individual
f
(
n
−
1
,
m
)
=
(
f
(
n
,
m
)
−
m
)
%
n
f(n-1, m) = (f(n, m)-m)\%n
f(n−1,m)=(f(n,m)−m)%n.
Finally,
f
(
n
,
m
)
=
(
f
(
n
−
1
,
m
)
+
m
)
%
n
f(n, m) = (f(n-1, m)+m)\%n
f(n,m)=(f(n−1,m)+m)%n.