欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1884B.Haunted House
Haunted House
You are given a number in binary representation consisting of exactly bits, possibly, with leading zeroes. For example, for the number will be given as , and for the number will be given as .
Let's fix some integer such that . 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 , or say that it is impossible.
Please note that for each you are solving the problem independently.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains one integer () — the length of the binary representation of the number.
The second line of each test case contains a string of length , consisting of and , — the binary representation of the number.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, for each output the smallest number of operations required to make the number divisible by , or output if it is impossible.
Note
In the first test case, we cannot swap any elements, and the number is not divisible by .
In the second test case, the initial number is . It is not divisible by , but if we perform the operation, then we obtain the number with binary representation , which is equal to in decimal representation, thus, it is divisible by . But this number is not divisible by and we cannot obtain any other number using the operations.
In the third test case, the initial number is . For we do not have to perform any operations since the initial number is divisible by . For we can perform one operation swapping the first two bits (we would obtain in binary representation, which corresponds to number ). There is no answer for .
Samples
6
1
1
2
01
3
010
5
10101
7
0000111
12
001011000110
-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
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |