-
Notifications
You must be signed in to change notification settings - Fork 91
/
josephus_problem_1.cpp
78 lines (53 loc) · 1.36 KB
/
josephus_problem_1.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define time cerr<<"Time taken : " <<(float)clock()/CLOCKS_PER_SEC <<" secs"<<"\n" ;
#define F first
#define S second
#define pb push_back
typedef long long int ll ;
const ll INF = (ll)1e18 ;
const ll MOD = (ll)1e9 + 7 ;
void solve() {
int n;
cin >> n ;
vector<int>arr(n) ;
for(int i=0 ; i<n ; i++){
arr[i]=i+1;
}
while(arr.size() > 1 ){
vector<int>s ;
for(int i=0 ;i<arr.size() ;i++){
if(i%2){
cout << arr[i] <<" " ;
}
else{
s.pb(arr[i]) ;
}
}
if(arr.size()%2==1){
int last = s.back() ;
s.pop_back() ;
//trace(last) ;
arr.clear() ;
arr.pb(last) ;
for(int i=0; i<s.size(); i++){
arr.pb(s[i]) ;
}
}
else
if(arr.size() % 2 == 0){
arr = s;
}
}
cout << arr[0] <<"\n" ;
}
int32_t main() {
fast ; time;
int t = 1;
// cin >> t;
while (t--) {
solve() ;
}
return 0 ;
}