You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 elements ai and aj in which ai is divisible by aj and transform ai to aj.

A number x is said to be divisible by a number y if x can be divided by y and the result is an exact whole number. For example, 15 is divisible by 3, because 15÷ 3 = 5 exactly, but 9 is not divisible by 2 because 9÷ 2 is 4 with 1 left over.

Your task is to find the minimum sum of the array a that can be obtained by making as many transform operations as you want. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), in which n is the size of array a. Then a line follows containing n integers a1, ..., an (1 ≤ ai ≤ 106), giving array a.

The sum of n overall test cases does not exceed 3 × 106.

Output

For each test case, print a single line containing the minimum sum of the array a that can be obtained after making as many transform operations as you want.

Example
Input
1
5
2 2 3 6 6
Output

11
题解:有一个长度为n的数组,对于数组中的两个元素x,y如果满足y%x==0,则可以将y转换成x,求经过多次变换后数组的前n项和最小是多少。由于数据量很大,我们可以用map(去重),对容器中的每一个元素便历,对x的每一因子xi从小到大枚举,如果xi在容器中,则将x转换成xi,并结束循环,在求x的因子时如果将因子存进数组中在sort,会超时,所以我用了一个小技巧,闲话少说看代码。还需要注意的是mp.erase(),当在迭代器中调用这个函数的话,需要先返回上一个迭代器,不然指针会变为一个野指针(可参考链表理解),我在这个地方卡了好久哦。

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<iostream>
#include<map>
typedef long long ll;
const int MAXN=+;
const int INF=1e6;
using namespace std;
map<ll,ll>::iterator it;
ll dp[];
ll l=;
int main()
{
ll m,n,t,i;
//cout<<INF;
scanf("%lld",&t);
map<ll,ll>mp;
while(t--)
{
mp.clear();
scanf("%lld",&m);
for(ll i=; i<m; i++)
{
scanf("%lld",&n);
mp[n]++;
}
ll ans=,flag=;
for(it=mp.begin(); it!=mp.end(); it++)
{
ll k=it->first;
if(k==)
{
flag=;
break;
}
ll flag=,ppq=;
for(i=;i*i<=k;i++)
{
if(k%i==)
{
if(mp.count(i))
{
mp[i]+=mp[k];
it--;
mp.erase(k);
flag=;
break;
}
else if(mp.count(k/i))
{
ppq=k/i;
}
}
}
if(!flag)
{
if(ppq)
{
mp[ppq]+=mp[k];
it--;
mp.erase(k);
}
}
}
if(flag)
printf("%lld\n",m);
else
{
for(it=mp.begin(); it!=mp.end(); it++)
{
ans+=it->second*it->first;
}
cout<<ans<<endl;
}
}
return ;
}

Minimum Sum of Array(map)的更多相关文章

  1. Minimum Sum of Array(map迭代器)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  2. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...

  4. 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...

  5. HDU 3473 Minimum Sum(划分树)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. JS中集合对象(Array、Map、Set)及类数组对象的使用与对比(转载)

    在使用js编程的时候,常常会用到集合对象,集合对象其实是一种泛型,在js中没有明确的规定其内元素的类型,但在强类型语言譬如Java中泛型强制要求指定类型. ES6引入了iterable类型,Array ...

  7. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  8. Minimum Sum(思维)

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted ...

  9. Minimum Sum LCM(uva10791+和最小的LCM+推理)

    L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

随机推荐

  1. L131

    Fake, Low Quality Drugs Come at High CostAbout one in eight essential medicines in low- and middle-i ...

  2. Android快速开发-选项卡

    介绍 几行代码实现Android选项卡界面,支持标准底部Tab自定义视图选项卡,头部文字选项卡. 底部自定义视图选项卡 先来看看实现下图中的效果我们代码应该怎么写? 实现上图效果只需以下代码: pub ...

  3. BZOJ3214 [Zjoi2013]丽洁体

    题意 平时的练习和考试中,我们经常会碰上这样的题:命题人给出一个例句,要我们类比着写句子.这种往往被称为仿写的题,不单单出现在小学生的考试中,也有时会出现在中考中.许多同学都喜欢做这种题,因为较其它题 ...

  4. [WPF]控件应用多个样式(转)

    最近在做WPF项目,公司没有专门的UI工程师,什么都要自己做.接触WPF已经有好几年了,自定义样式什么的也可以做一些.WPF在使用样式的时候一般都是 Style="{StaticResour ...

  5. 系列文章--SharePoint 2013 Designer 入门教程

    SharePoint的使用中,SharePoint Designer是非常重要的工具,我们可以通过Designer设计页面.母版页,维护.管理站点,也可以定制列表表单.数据视图,设计工作流等等.下面总 ...

  6. hive字段原理--有删除一列想到的

    hive删除一张表的字段不会动数据文件,只是修改了一下metadata表里面的表定义:所以会出现一种情况:就是这张表如果之前数据是满的(个格列都有数据),那么被删除的那列后数据都往前窜了一个,最后一个 ...

  7. Java 数据类型及转换

    整形: byte(1个字节) 范围:-128~127 short(2个字节) 范围:-215~215-1 (-32768~32767) int(4个字节) 范围:-231~231-1 (-214748 ...

  8. android中asynctask的使用实例

    参考此blog写的非常的好http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html MainActivity.java imp ...

  9. sqlbulkcopy 使用DataTable作为数据源的数据类型问题--来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier

    今天做批量插入的时候,SQLSERVER总是报错,错误提示“来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier”. 首先核对了一下定义的dataTable ...

  10. nginx 安装echo模块

    学习资源: https://www.cnblogs.com/xwupiaomiao/p/7997938.html https://blog.csdn.net/hb1707/article/detail ...