AT_arc161_b 题解
shinzAnmono · · 题解
[ARC161B] Exactly Three Bits
首先,我们可知,
然后我们对每个
-
\mathrm{popcount}(s)=1$,也就是说二进制串的形式是 $1\underset{n个0}{00\cdots00}$,那么我们可以那么答案便是 $111\underset{n-4个0}{00\cdots00} -
-
-
我们对二进制串
long long stoll(const std::string &__str, std::size_t *__idx, int __base)
这个函数可以将字符串转为 long long 型,__idx 一般使用 nullptr,base 是字符串的进制,默认为
给出代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
using ll = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
ll n;
std::cin >> n;
if (n < 7) std::cout << "-1\n";
else {
std::stack<ll> s;
std::string bit;
if (n < 7) std::cout << "-1\n";
ll cx = n;
while (cx)
s.push(cx % 2), cx /= 2ll;
while (!s.empty())
bit += s.top() + '0', s.pop();
int cnt = 0;
for (char c: bit)
if (c == '1') cnt++;
if (cnt < 3) {
if (cnt == 1) bit = "111" + std::string(bit.length() - 4, '0');
else {
int pos = bit.find('1', bit.find('1') + 1);
if (pos > bit.length() - 3)
bit = "111" + std::string(bit.length() - 4, '0');
else bit[pos] = '0', bit[pos + 1] = bit[pos + 2] = '1';
}
}
else {
int pos = bit.find('1', bit.find('1', bit.find('1') + 1) + 1);
for (int i = pos + 1; i < bit.size(); i++) bit[i] = '0';
}
std::cout << std::stoll(bit, nullptr, 2) << "\n";
}
}
return 0;
}