CF1789D.Serval and Shift-Shift-Shift

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Serval has two nn-bit binary integer numbers aa and bb. He wants to share those numbers with Toxel.

Since Toxel likes the number bb more, Serval decides to change aa into bb by some (possibly zero) operations. In an operation, Serval can choose any positive integer kk between 11 and nn, and change aa into one of the following number:

  • a⊕(a≪k)a\oplus(a\ll k)
  • a⊕(a≫k)a\oplus(a\gg k)

In other words, the operation moves every bit of aa left or right by kk positions, where the overflowed bits are removed, and the missing bits are padded with 00. The bitwise XOR of the shift result and the original aa is assigned back to aa.

Serval does not have much time. He wants to perform no more than nn operations to change aa into bb. Please help him to find out an operation sequence, or determine that it is impossible to change aa into bb in at most nn operations. You do not need to minimize the number of operations.

In this problem, x⊕yx\oplus y denotes the bitwise XOR operation of xx and yy. a≪ka\ll k and a≫ka\gg k denote the logical left shift and logical right shift.

Serval 有两个 nn 位二进制整数 aa 和 bb。他希望将这两个数分享给 Toxel。

由于 Toxel 更喜欢数字 bb,Serval 决定通过若干次(可能为零次)操作将 aa 变为 bb。每次操作中,Serval 可以任选一个介于 11 到 nn 之间的正整数 kk,并将 aa 替换为以下两个数之一:

  • a⊕(a≪k)a\oplus(a\ll k)
  • a⊕(a≫k)a\oplus(a\gg k)

换言之,该操作将 aa 的每一位向左或向右移动 kk 个位置,移出边界的位被丢弃,空缺位置补 00;然后将移位结果与原 aa 进行按位异或,并将结果赋回给 aa。

Serval 时间有限,他希望至多执行 nn 次操作便将 aa 变为 bb。请帮助他找出一个满足要求的操作序列,或判定在至多 nn 次操作内无法将 aa 变为 bb。你无需最小化操作次数。

本题中,x⊕yx\oplus y 表示 xx 与 yy 的按位异或运算;a≪ka\ll k 和 a≫ka\gg k 分别表示逻辑左移和逻辑右移。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1031\le t\le2\cdot10^{3}). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1031\le n\le2\cdot10^{3}) — the number of bits in numbers aa and bb.

The second and the third line of each test case contain a binary string of length nn, representing aa and bb, respectively. The strings contain only characters 0 and 1.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1032\cdot10^{3}.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤2⋅1031\le t\le2\cdot10^{3})。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1031\le n\le2\cdot10^{3})—— 表示数字 aa 和 bb 的二进制位数。

每个测试用例的第二行和第三行分别包含一个长度为 nn 的二进制字符串,表示 aa 和 bb。这些字符串仅由字符 0 和 1 组成。

保证所有测试用例的 nn 之和不超过 2⋅1032\cdot10^{3}。

输出格式

For each test case, if it is impossible to change aa into bb in at most nn operations, print a single integer −1-1.

Otherwise, in the first line, print the number of operations mm (0≤m≤n0\le m\le n).

If m>0m \gt 0, in the second line, print mm integers k1,k2,…,kmk_{1},k_{2},\dots,k_{m} representing the operations. If 1≤ki≤n1\le k_{i}\le n, it means logical left shift aa by kik_{i} positions. If −n≤ki≤−1-n\le k_{i}\le-1, it means logical right shift aa by −ki-k_{i} positions.

If there are multiple solutions, print any of them.

对于每个测试用例,如果无法在至多 nn 次操作内将 aa 变为 bb,则输出单个整数 −1-1。

否则,在第一行输出操作次数 mm(0≤m≤n0\le m\le n)。

若 m>0m \gt 0,则在第二行输出 mm 个整数 k1,k2,…,kmk_{1},k_{2},\dots,k_{m},表示所执行的操作:若 1≤ki≤n1\le k_{i}\le n,表示对 aa 进行逻辑左移 kik_{i} 位;若 −n≤ki≤−1-n\le k_{i}\le-1,表示对 aa 进行逻辑右移 −ki-k_{i} 位。

若存在多种解法,输出任意一种即可。

输入输出样例

  • 输入#1

    3
    5
    00111
    11000
    1
    1
    1
    3
    001
    000

    输出#1

    2
    3 -2
    0
    -1

说明/提示

In the first test case:

The first operation changes aa into \require{cancel}00111\oplus\cancel{001}11\underline{000}=11111.

The second operation changes aa into \require{cancel}11111\oplus\underline{00}111\cancel{11}=11000.

The bits with strikethroughs are overflowed bits that are removed. The bits with underline are padded bits.

In the second test case, aa is already equal to bb, so no operations are needed.

In the third test case, it can be shown that aa cannot be changed into bb.

在第一个测试用例中:

第一次操作将 aa 变为 \require{cancel}00111\oplus\cancel{001}11\underline{000}=11111。

第二次操作将 aa 变为 \require{cancel}11111\oplus\underline{00}111\cancel{11}=11000。

带删除线的位是溢出而被移除的位;带下划线的位是补零填充的位。

在第二个测试用例中,aa 已经等于 bb,因此无需任何操作。

在第三个测试用例中,可以证明 aa 无法变为 bb。

输入解题思路,AI测评打分。不知道怎么写?

首页