Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Do not think about the operations one by one. Since flipping bits commutes, only the set of chosen indices and its size matter.
Suppose you choose exactly indices. A chosen position is flipped times, while an unchosen position is flipped times.
Only parity matters. If is even, chosen positions are flipped odd times and unchosen positions are flipped even times.
For even , the chosen indices must be exactly the positions containing 1. For odd , the chosen indices must be exactly the positions containing 0.
So there are only two candidate answers: choose all 1 positions if their count is even, or choose all 0 positions if their count is odd. If neither works, it is impossible.
The operation looks annoying because choosing index leaves alone and flips everything else. The trick is to stop simulating. The order of operations does not matter: each position only cares how many times it gets flipped.
Assume we perform exactly operations, choosing a set of distinct indices.
For a position :
Only parity matters, because flipping twice cancels out.
Now split by parity of .
If is even:
1 to become 0.0.Therefore, when is even, the chosen set is forced: it must be exactly all positions where . This works exactly when the number of ones is even.
If is odd:
0.1.Therefore, when is odd, the chosen set is also forced: it must be exactly all positions where . This works exactly when the number of zeros is odd.
So the entire problem collapses to two possible constructions:
1s is even, choose every 1 position.0s is odd, choose every 0 position.There is no hidden third option. Once 's parity is fixed, every index is forced by its starting bit. If both candidate parities fail, the string is cooked.
Edge cases:
0 needs operations, while 1 is impossible.The algorithm just scans the string, stores positions of zeros and ones, then prints one of the two valid lists.
Complexity is per test case, with extra memory for the answer indices.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
vector<int> zeros, ones;
for (int i = 0; i < n; i++) {
if (s[i] == '0') zeros.push_back(i + 1);
else ones.push_back(i + 1);
}
vector<int> ans;
if (ones.size() % 2 == 0) {
ans = ones;
} else if (zeros.size() % 2 == 1) {
ans = zeros;
} else {
cout << -1 << '\n';
continue;
}
cout << ans.size() << '\n';
for (int idx : ans) cout << idx << ' ';
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
vector<int> zeros, ones;
for (int i = 0; i < n; i++) {
if (s[i] == '0') zeros.push_back(i + 1);
else ones.push_back(i + 1);
}
vector<int> ans;
if (ones.size() % 2 == 0) {
ans = ones;
} else if (zeros.size() % 2 == 1) {
ans = zeros;
} else {
cout << -1 << '\n';
continue;
}
cout << ans.size() << '\n';
for (int idx : ans) cout << idx << ' ';
cout << '\n';
}
return 0;
}