-
Notifications
You must be signed in to change notification settings - Fork 6
/
josephus.cpp
61 lines (46 loc) · 1.24 KB
/
josephus.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
#include<bits/stdc++.h>
#define PI acos(-1)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define lli long long int
#define ull unsigned long long int
#define pii pair < int, int>
#define pll pair < ll, ll>
#define sc scanf
#define scin(x) sc("%d",&(x))
#define scln(x) sc("%lld",&(x))
#define pf printf
#define ms(a,b) memset(a,b,sizeof(a))
#define veci vector<int>
#define vecl vector<long long int>
#define vecp vector< pair<int,int> >
#define pb push_back
using namespace std;
int findmsb(int n)
{
int position=0;
while(n!=0)
{
position++;
n=n>>1 ;
}
return position;
}
int PersonToLive(int n)
{
int length = findmsb(n);
int xor_oparand = 1 << (length-1);
n = n ^ xor_oparand;
n = n << 1;
n = n|1;
return n;
}
int main()
{
int NumberOfSoldiers;
cin >> NumberOfSoldiers;
cout << PersonToLive(NumberOfSoldiers) << endl;
return 0;
}