forked from derekhh/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalf.cpp
More file actions
42 lines (40 loc) · 679 Bytes
/
half.cpp
File metadata and controls
42 lines (40 loc) · 679 Bytes
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
//half.cpp
//A stones game
//Weekly Challenges - Week 3
//Author: derekhh
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
if (n % 2 == 1)
printf("%d\n", 1);
else
{
int cnt = 0;
while (n) n >>= 1, cnt++;
int finalsg = 1 ^ cnt, ans = -1;
for (int i = 2; i <= cnt; i++)
{
if ((i ^ finalsg) <= i)
{
int a = (i ^ finalsg);
int val = (1 << (i - 1)) - ((1 << a) - 1);
if (val < (1 << (i - 2)))
val = (1 << (i - 2));
if (val < ans || ans == -1)
ans = val;
}
}
printf("%d\n", ans);
}
}
return 0;
}