CF1884B.Haunted House

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a number in binary representation consisting of exactly nn bits, possibly, with leading zeroes. For example, for n=5n = 5 the number 66 will be given as 0011000110, and for n=4n = 4 the number 99 will be given as 10011001.

Let's fix some integer ii such that 1≤i≤n1 \le i \le n. In one operation you can swap any two adjacent bits in the binary representation. Your goal is to find the smallest number of operations you are required to perform to make the number divisible by 2i2^i, or say that it is impossible.

Please note that for each 1≤i≤n1 \le i \le n you are solving the problem independently.

你将得到一个恰好由 nn 位二进制数字组成的数(可能包含前导零)。例如,当 n=5n = 5 时,数字 66 表示为 0011000110;当 n=4n = 4 时,数字 99 表示为 10011001。

现固定某个整数 ii,满足 1≤i≤n1 \le i \le n。在一次操作中,你可以交换二进制表示中任意两个相邻的比特位。你的目标是:求出使该数能被 2i2^i 整除所需的最少操作次数;若不可能实现,则说明其不可行。

请注意:对每个满足 1≤i≤n1 \le i \le n 的 ii,你均需独立求解该问题。

输入格式

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

The first line of each test case contains one integer nn (1≤n≤1051 \le n \le 10^5) — the length of the binary representation of the number.

The second line of each test case contains a string of length nn, consisting of 00 and 11, — the binary representation of the number.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)——该数字二进制表示的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串,仅由字符 00 和 11 组成——即该数字的二进制表示。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, for each 1≤i≤n1 \le i \le n output the smallest number of operations required to make the number divisible by 2i2^i, or output −1-1 if it is impossible.

对于每个测试用例,对每个 1≤i≤n1 \le i \le n,输出使该数能被 2i2^i 整除所需的最少操作次数;若不可能,则输出 −1-1。

输入输出样例

  • 输入#1

    6
    1
    1
    2
    01
    3
    010
    5
    10101
    7
    0000111
    12
    001011000110

    输出#1

    -1 
    1 -1 
    0 1 -1 
    1 3 -1 -1 -1 
    3 6 9 12 -1 -1 -1 
    0 2 4 6 10 15 20 -1 -1 -1 -1 -1

说明/提示

In the first test case, we cannot swap any elements, and the number 11 is not divisible by 22.

In the second test case, the initial number is 11. It is not divisible by 22, but if we perform the operation, then we obtain the number with binary representation 1010, which is equal to 22 in decimal representation, thus, it is divisible by 22. But this number is not divisible by 44 and we cannot obtain any other number using the operations.

In the third test case, the initial number is 22. For i=1i = 1 we do not have to perform any operations since the initial number is divisible by 22. For i=2i = 2 we can perform one operation swapping the first two bits (we would obtain 100100 in binary representation, which corresponds to number 44). There is no answer for i=3i = 3.

在第一个测试用例中,我们无法交换任何元素,且数字 11 不能被 22 整除。

在第二个测试用例中,初始数字为 11。它不能被 22 整除,但如果执行一次操作,则得到二进制表示为 1010 的数字,其十进制值为 22,因此可被 22 整除。但该数字不能被 44 整除,且通过所允许的操作无法得到其他数字。

在第三个测试用例中,初始数字为 22。当 i=1i = 1 时,无需执行任何操作,因为初始数字已可被 22 整除;当 i=2i = 2 时,可执行一次操作,交换前两位(得到二进制表示 100100,对应十进制数 44);当 i=3i = 3 时,无解。

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

首页