CF1734C.Removing Smallest Multiples

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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 1≤k≤n1 \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.

给你一个集合 SS,其中包含前 nn 个正整数:1,2,…,n1, 2, \ldots, n。

你可以对 SS 执行以下操作任意多次(也可以不执行):

  • 选择一个满足 1≤k≤n1 \le k \le n 的正整数 kk,使得 SS 中存在 kk 的倍数;然后,从 SS 中删除最小的 kk 的倍数。该操作的代价为 kk。

再给你一个集合 TT,它是 SS 的一个子集。求将 SS 变换为 TT 所需的最小总代价。可以证明,这样的变换总是可行的。

输入格式

The first line of the input contains a single integer tt (1≤t≤10 0001 \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 (1≤n≤1061 \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.

输入的第一行包含一个整数 tt(1≤t≤10 0001 \le t \le 10\,000),表示测试用例的数量。随后是各测试用例的描述。

每组测试用例的第一行包含一个正整数 nn(1≤n≤1061 \le n \le 10^6)。

每组测试用例的第二行包含一个长度为 nn 的二进制字符串,用于描述集合 TT。该字符串的第 ii 个字符为 '1' 当且仅当 i∈Ti \in T;否则为 '0'。

保证所有测试用例的 nn 值之和不超过 10610^6。

输出格式

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

对于每个测试用例,输出一个非负整数——使得字符串 SS 被转换为字符串 TT 所需操作的最小总代价。

输入输出样例

  • 输入#1

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

    输出#1

    0
    11
    4
    4
    17
    60

说明/提示

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,7S = {1, 2, 3, 4, 5, 6, 7}, and T=1,2,4,7T = {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,4S = {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,4S = {1, 2, 3, 4} and T=3T = {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.

在第一个测试用例中,我们无需执行任何操作,因为 SS 已经等于 TT,即集合 1,2,3,4,5,6{1, 2, 3, 4, 5, 6}。

在第二个测试用例中,初始时 S=1,2,3,4,5,6,7S = {1, 2, 3, 4, 5, 6, 7},而 T=1,2,4,7T = {1, 2, 4, 7}。我们将执行以下操作:

  1. 选择 k=3k=3,然后从 SS 中删除 33。
  2. 选择 k=3k=3,然后从 SS 中删除 66。
  3. 选择 k=5k=5,然后从 SS 中删除 55。

总代价为 3+3+5=113+3+5 = 11。可以证明这是可能的最小代价。

在第三个测试用例中,初始时 S=1,2,3,4S = {1, 2, 3, 4},而 T=T = {}(空集)。我们将执行 44 次 k=1k=1 的操作,依次删除 11、22、33 和 44。

在第四个测试用例中,初始时 S=1,2,3,4S = {1, 2, 3, 4},而 T=3T = {3}。我们将先执行两次 k=1k=1 的操作,删除 11 和 22;然后执行一次 k=2k=2 的操作,删除 44。

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

首页