CF1884B.Haunted House
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a number in binary representation consisting of exactly n bits, possibly, with leading zeroes. For example, for n=5 the number 6 will be given as 00110, and for n=4 the number 9 will be given as 1001.
Let's fix some integer i such that 1≤i≤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 2i, or say that it is impossible.
Please note that for each 1≤i≤n you are solving the problem independently.
你将得到一个恰好由 n 位二进制数字组成的数(可能包含前导零)。例如,当 n=5 时,数字 6 表示为 00110;当 n=4 时,数字 9 表示为 1001。
现固定某个整数 i,满足 1≤i≤n。在一次操作中,你可以交换二进制表示中任意两个相邻的比特位。你的目标是:求出使该数能被 2i 整除所需的最少操作次数;若不可能实现,则说明其不可行。
请注意:对每个满足 1≤i≤n 的 i,你均需独立求解该问题。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains one integer n (1≤n≤105) — the length of the binary representation of the number.
The second line of each test case contains a string of length n, consisting of 0 and 1, — the binary representation of the number.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105)——该数字二进制表示的长度。
每个测试用例的第二行包含一个长度为 n 的字符串,仅由字符 0 和 1 组成——即该数字的二进制表示。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, for each 1≤i≤n output the smallest number of operations required to make the number divisible by 2i, or output −1 if it is impossible.
对于每个测试用例,对每个 1≤i≤n,输出使该数能被 2i 整除所需的最少操作次数;若不可能,则输出 −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 1 is not divisible by 2.
In the second test case, the initial number is 1. It is not divisible by 2, but if we perform the operation, then we obtain the number with binary representation 10, which is equal to 2 in decimal representation, thus, it is divisible by 2. But this number is not divisible by 4 and we cannot obtain any other number using the operations.
In the third test case, the initial number is 2. For i=1 we do not have to perform any operations since the initial number is divisible by 2. For i=2 we can perform one operation swapping the first two bits (we would obtain 100 in binary representation, which corresponds to number 4). There is no answer for i=3.
在第一个测试用例中,我们无法交换任何元素,且数字 1 不能被 2 整除。
在第二个测试用例中,初始数字为 1。它不能被 2 整除,但如果执行一次操作,则得到二进制表示为 10 的数字,其十进制值为 2,因此可被 2 整除。但该数字不能被 4 整除,且通过所允许的操作无法得到其他数字。
在第三个测试用例中,初始数字为 2。当 i=1 时,无需执行任何操作,因为初始数字已可被 2 整除;当 i=2 时,可执行一次操作,交换前两位(得到二进制表示 100,对应十进制数 4);当 i=3 时,无解。
输入解题思路,AI测评打分。不知道怎么写?