-
Notifications
You must be signed in to change notification settings - Fork 7
/
BIT2.cpp
57 lines (57 loc) · 788 Bytes
/
BIT2.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
#include "bits/stdc++.h"
#define ll unsigned long long
#define MAXN 555555555555555
using namespace std;
ll sum(ll n) {
ll ans = 0;
int x = 0;
for (int i = __lg(n); i >= 0; --i)
if (n & 1LLU << i)
ans += ((1LLU << i) >> 1) * i + 1 + (1LLU << i) * x++;
return ans;
}
ll Binary_Search(ll n)
{
ll low = 0;
ll high = n+1;
ll mid;
while (low <= high)
{
mid = ((low + high) >> 1);
ll temp = sum(mid);
if (temp == n)
{
return mid;
}
else if (n > temp)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
// READ;
int t;
scanf("%d", &t);
while (t--)
{
ll n;
scanf("%llu", &n);
ll ans = Binary_Search(n);
if (ans == -1)
{
printf("Does Not Exist.\n");
}
else
{
printf("%llu\n", ans);
}
}
return 0;
}