forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1527A - And Then There Were K.cpp
97 lines (64 loc) · 1.39 KB
/
1527A - And Then There Were K.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Given an integer n, find the maximum value of integer k such that the following condition holds:
n & (n-1) & (n-2) & (n-3) & ... (k) = 0
where & denotes the bitwise AND operation.
Input
The first line contains a single integer t (1=t=3·104). Then t test cases follow.
The first line of each test case contains a single integer n (1=n=109).
Output
For each test case, output a single integer — the required integer k.
Example
inputCopy
3
2
5
17
outputCopy
1
3
15
Note
In the first testcase, the maximum value for which the continuous & operation gives 0 value, is 1.
In the second testcase, the maximum value for which the continuous & operation gives 0 value, is 3. No value greater then 3, say for example 4, will give the & sum 0.
5&4?0,
5&4&3=0.
Hence, 3 is the answer.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long p= log2(n);
long long res = pow(2, p);
cout << res - 1 << "\n";
}
return 0;
}
/*
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int cnt = 0;
while (n != 1) {
cnt++;
n /= 2;
}
long long res = (long long)pow(2, cnt) - 1;
cout << res << "\n";
}
return 0;
}
*/