-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJosephus_Problem_II.cpp
41 lines (39 loc) · 1.11 KB
/
Josephus_Problem_II.cpp
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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define dbg(x) cout << #x << " " << x << endl;
/*
use an ordered set to find an element by its index
skip k elements and fetch (k+1)th element in each iteration
*/
int32_t main() {
int n, k;
cin >> n >> k;
ordered_set st;
for (int i = 1; i <= n; i++)
st.insert(i);
int start = k + 1;
while (st.size() > 0) {
vector<int> values;
int last = 0;
for (int i = start; i <= (int)st.size(); i += k + 1) {
int val = *st.find_by_order(i - 1);
cout << val << " ";
values.push_back(val);
last = i;
}
start = (k + 1) - (st.size() - last);
for (int v : values)
st.erase(v);
if (st.size() == 0)
break;
if (start % st.size() == 0)
start = st.size();
else
start %= st.size();
}
return 0;
}