问题 G: Segment Tree
时间限制: 1 Sec 内存限制: 512 MB
提交: 74 解决: 17
[提交] [状态] [命题人:admin]
题目描述
Mcginn opens the code which he wrote 25 years ago.
Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach whencall function build(1 , 1 , n) in the main function is m.
There are T test cases. Each case contains one integer m.
输入
Input is given from Standard Input in the following format:
T
m1
m2
.
.
.
mT
Constraints
1 ≤ T ≤ 100
1 ≤ m ≤ 1018
输出
For each m print one line denotes the answer.
样例输入
复制样例数据
3 3 4 5
样例输出
1 0 1
二进制打表找规律
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
ll f[100];
int main() {
int t;
f[0] = 1;
for (int i = 1; i <= 64; i++)
f[i] = f[i - 1] * 2;
cin >> t;
while (t--) {
ll n;
cin >> n;
if (n % 2 == 0) {
cout << "0" << endl;
continue;
}
if (n == 1) {
cout << "1" << endl;
continue;
}
int flag = -1, cnt = 0, v = 0;
for (ll i = 63; i >= 0; i--) {
if (n & f[i]) {
cnt++;
if (flag == -1) flag = 1;
else if (flag == 0) flag = 2, v++;
else if (flag==2) v++;
} else {
if (flag == 1) flag = 0;
}
}
if (flag == 1) cout << "1" << endl;
else {
if (v == 1) cout << f[cnt - 2] << endl;
else cout << "0" << endl;
}
}
return 0;
}