cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

josephus_problem_i.cpp (756B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 void solve()
     19 {
     20 	int n;
     21 	cin >> n;
     22 	set<int> res;
     23 	for (int i = 1; i <= n; i++) {
     24 		res.insert(i);
     25 	}
     26 
     27 	auto last = res.begin();
     28 	for (int i = 1; i <= n; i++) {
     29 		last++;
     30 		if (last == res.end()) {
     31 			last = res.begin();
     32 		}
     33 		cout << *last << " ";
     34 		auto l2 = last;
     35 		l2++;
     36 		res.erase(last);
     37 		if (l2 == res.end()) {
     38 			l2 = res.begin();
     39 		}
     40 		last = l2;
     41 	}
     42 	cout << "\n";
     43 }
     44 
     45 int main()
     46 {
     47 	ios::sync_with_stdio(0);
     48 	cin.tie(0);
     49 	solve();
     50 }