https://cses.fi/paste/9abb1e3c5422a39119bcda/
#include<bits/stdc++.h> using namespace std; #define ll long long void josephus(ll n, ll k, set<ll> &v) { ll i = 0; set<ll> :: iterator it; while(v.size() > 0) { i = (i+k)%v.size(); it = v.begin(); advance(it, i); cout << *it << " "; v.erase(it); } } int main() { ll n, k; cin >> n >> k; set<ll> v; for (ll i = 0; i < n; i++) { v.insert(i+1); } josephus(n, k, v); return 0; }