801 字
4 分钟
Flipping-Binary-String

题面#

You are given a binary string ss of length nn. You can perform the following operation on the string:

  • Choose an index ii, and flip the bit present at all other indices except for the index ii. In other words, choose an integer ii (1in1 \le i \le n), and for all jj such that 1jn1 \le j \le n and jij \ne i, if sj=0s_j = 0, then set sj:=1s_j:=1, otherwise set sj:=0s_j:=0.

You can perform the operation any number of times, but each index can be chosen by at most one operation.

Your task is to make all bits present in the string ss equal 00, or report that it is impossible to do so. You don’t have to minimize the number of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5).

The second line of each test case contains the binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output 1-1 if it is impossible to transform all bits to 00. Otherwise, output two lines in the following format:

  • In the first line, print the number of operations xx (0xn0 \leq x \leq n).
  • In the second line of each test case, print xx numbers – the indices you select in each operation in order. You should guarantee that each index is chosen at most once.

If there are multiple possible solutions, you may output any.

思路#

这题最关键的是把“操作”翻译成按位异或关系

我们设一个变量 xi{0,1}x_i \in \{0, 1\},表示下标 ii 是否被选择进行操作:

  • xi=1x_i = 1:这个下标被选过一次
  • xi=0x_i = 0:这个下标没被选

对于任意一个位置 jj,它会被所有 iji \ne j 的操作翻转,因此最终状态满足:

sj=sjijxis'_j = s_j \oplus \bigoplus_{i \ne j} x_i

我们再定义总异或:

K=i=1nxiK = \bigoplus_{i=1}^{n} x_i

那么就有:

ijxi=Kxj\bigoplus_{i \ne j} x_i = K \oplus x_j

题目要求最终全为 00,即 sj=0s'_j = 0,代入可得:

xj=sjKx_j = s_j \oplus K

这句话很重要:只要 KK 确定,所有 xjx_j 都被唯一确定

接下来做可行性判断,对上式整体异或:

K=j=1nxj=j=1n(sjK)=(j=1nsj)(nmod2)KK = \bigoplus_{j=1}^{n} x_j = \bigoplus_{j=1}^{n} (s_j \oplus K) = \left(\bigoplus_{j=1}^{n} s_j\right) \oplus (n \bmod 2) \cdot K

P=sjP = \bigoplus s_j(也就是字符串里 11 的个数奇偶性),分情况:

  • nn 为偶数时:K=PK = P,一定有解
  • nn 为奇数时:推出必须 P=0P = 0,否则无解

因此无解条件只有一个:nn 为奇数且 11 的个数为奇数

构造方式也就很直接:

  1. 先求 PP
  2. 如果 nn 是奇数且 P=1P=1,输出 1-1
  3. 否则可行,取
    • 偶数 nnK=PK = P
    • 奇数 nn:可取 K=0K = 0
  4. 按照 xj=sjKx_j = s_j \oplus K 逐位计算,若 xj=1x_j = 1,就把下标 j+1j+1 放进答案

最终答案数组里的下标就是要执行操作的位置,每个下标最多出现一次,完全符合题意

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n;
string s;
cin >> n >> s;
int pp = 0;
for (auto c: s) {
if (c == '1') pp ++;
}
int p = pp % 2;
if (n % 2 != 0 && p == 1) cout << -1 << endl;
else {
int k;
if (p == 1) k = 1;
else k = p;
vector<int> ans, x;
for (int i = 0; i < n; i ++) {
x.push_back((s[i] - '0') ^ k);
if (x[i] == 1) ans.push_back(i + 1);
}
cout << ans.size() << '\n';
for (int i = 0; i < (int)ans.size(); i++) {
cout << ans[i] << " \n"[i == (int)ans.size() - 1];
}
if (ans.empty()) cout << '\n';
}
}
}
Flipping-Binary-String
https://github.com/posts/flipping-binary-string/
作者
FZSGBall
发布于
2026-04-15
许可协议
CC BY-NC-SA 4.0