-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephus_Problem.cpp
More file actions
54 lines (46 loc) · 924 Bytes
/
Josephus_Problem.cpp
File metadata and controls
54 lines (46 loc) · 924 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
// Return last alive person, O(n)
int josephus(int n, int k) {
int res = 0;
for (int i = 1; i <= n; i++) {
res = (res + k) % i;
}
return res + 1;
}
// Returns the mth killed person, O(k*log(n))
int64_t josephus(int64_t n, int64_t k, int64_t m) {
assert(m <= n);
if (k <= 1) {
return m;
}
m = n - m;
int64_t i = m;
while (i < n) {
int64_t r = (i - m + k - 2) / (k - 1);
if ((i + r) > n) {
r = n - i;
}
else if (!r) {
r = 1;
}
i += r;
m = (m + (r * k)) % i;
}
return m + 1;
}
void solve() {
int n, k;
cin >> n >> k;
cout << josephus(n, k) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}