CF BUDDY
← Problems·

1734C · Removing Smallest Multiples

1200 · greedy, math

Problem: You are given a set SS, which contains the first nn positive integers: 1,2,,n1, 2, \ldots, n.

You can perform the following operation on SS any number of times (possibly zero):

  • Choose a positive integer kk where 1kn1 \le k \le n, such that there exists a multiple of kk in SS. Then, delete the smallest multiple of kk from SS. This operation requires a cost of kk.

You are given a set TT, which is a subset of SS. Find the minimum possible total cost of operations such that SS would be transformed into TT. We can show that such a transformation is always possible.

Input Format: The first line of the input contains a single integer tt (1t100001 \le t \le 10\,000) — the number of test cases. The description of the test cases follows.

The first line contains a single positive integer nn (1n1061 \le n \le 10^6).

The second line of each test case contains a binary string of length nn, describing the set TT. The ii-th character of the string is '1' if and only if ii is an element of TT, and '0' otherwise.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

Output Format: For each test case, output one non-negative integer — the minimum possible total cost of operations such that SS would be transformed into TT.

Note: In the first test case, we shall not perform any operations as SS is already equal to TT, which is the set {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\}.

In the second test case, initially, S={1,2,3,4,5,6,7}S = \{1, 2, 3, 4, 5, 6, 7\}, and T={1,2,4,7}T = \{1, 2, 4, 7\}. We shall perform the following operations:

  1. Choose k=3k=3, then delete 33 from SS.
  2. Choose k=3k=3, then delete 66 from SS.
  3. Choose k=5k=5, then delete 55 from SS.

The total cost is 3+3+5=113+3+5 = 11. It can be shown that this is the smallest cost possible.

In the third test case, initially, S={1,2,3,4}S = \{1, 2, 3, 4\} and T={}T = \{\} (empty set). We shall perform 44 operations of k=1k=1 to delete 11, 22, 33, and 44.

In the fourth test case, initially, S={1,2,3,4}S = \{1, 2, 3, 4\} and T={3}T = \{3\}. We shall perform two operations with k=1k=1 to delete 11 and 22, then perform one operation with k=2k=2 to delete 44.

Sample Cases

Case 1

Input

6
6
111111
7
1101001
4
0000
4
0010
8
10010101
15
110011100101100

Output

0
11
4
4
17
60

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback