C. Removing Smallest Multiples

time limit per test

2 seconds

memory limit per test

256 megabytes

You are given a set S, which contains the first n positive integers: 1,2,...,n.

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

  • Choose a positive integer k where 1≤k≤n, such that there exists a multiple of k in S. Then, delete the smallest multiple of k from S. This operation requires a cost of k.

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

Input

The first line of the input contains a single integer t (1≤t≤10000) --- the number of test cases. The description of the test cases follows.

The first line contains a single positive integer n (1≤n≤106).

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

It is guaranteed that the sum of n over all test cases does not exceed 106.

Output

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

Example

Input

Copy

复制代码

6

6

111111

7

1101001

4

0000

4

0010

8

10010101

15

110011100101100

Output

Copy

复制代码
0
11
4
4
17
60

Note

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

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

  1. Choose k=3, then delete 3 from S.
  2. Choose k=3, then delete 6 from S.
  3. Choose k=5, then delete 5 from S.

The total cost is 3+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} and T={} (empty set). We shall perform 4 operations of k=1 to delete 1, 2, 3, and 4.

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

解题说明:此题是一道模拟题,采用贪心算法,可以将输入数列当成字符串处理,然后循环遍历,每次找出等于0的数字,让其变成1。考虑到是k的最小倍数,内层循环中步长设置为k即可。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int arr[1000005];
char s[1000005];

int main()
{
    int t;
    scanf("%d", &t);

    while (t--) 
    {
        int n;
        scanf("%d", &n);
        scanf("%s", s + 1);

        for (int i = 1; i <= n; i++)
        {
            arr[i] = 0;
        }
        long long ans = 0;

        for (int k = 1; k <= n; k++)
        {
            for (int j = k; j <= n; j += k)
            {
                if (s[j] == '1')
                {
                    break;
                }
                if (!arr[j]) 
                {
                    arr[j] = 1;
                    ans += k;
                }
            }
        }

        printf("%lld\n", ans);
    }

    return 0;
}
相关推荐
BK贝壳7 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
吴名氏.7 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
codeejun8 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
小此方10 小时前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
瑶池酒剑仙11 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
郝学胜-神的一滴12 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
不积跬步无以至千里mmbb2612 小时前
【软件工艺师 rust相关课程】
开发语言·后端·rust
SEO_juper12 小时前
HTTPS 混合内容检查工具:页面全站 https 了,但还有 http 资源在拖后腿
开发语言·网络协议·http·https·node.js·json·bash
神一样的老师13 小时前
WS63 BLE 配网实战:用 Python 在 PC 上替代手机完成 WiFi 配网
开发语言·python·智能手机
geovindu15 小时前
python: Decision Tree
开发语言·后端·python·决策树