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. => 应用在js回调函数中

    => 可以简化以前的回调函数的调用,具体来说: 今后,几乎所有的回调函数都可用箭头函数简化 比如: 1. 所有回调函数都可: 去function改=> 2. 如果函数体只有一句话: 可省略 ...

  2. MPLS基础一

    多协议标签交换(MPLS) 是一种用于快速数据包交换和路由的体系,具有管理各种不同形式通信流的机制. 内容:RID     /     MTU     /      认证    /     TTL   ...

  3. PenMount Touch显示鼠标指针驱动安装

    /******************************************************************************* * PenMount Touch显示鼠 ...

  4. 关于this指向问题的总结【转自秘密花园】

    this 的工作原理 JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 第一种:全局范围内 this; 当在全部范围内使用  ...

  5. 记一次印象有点深刻的坑(bug)

    1.该模块的主要功能介绍 该系统是一个网上课程预约系统. 学生进行口语预约(没上课前可以进行取消)--> 等待上课 --> 上完课老师进行完成的确认操作. 2.问题描述 看下图,现在出现的 ...

  6. Maven构建跳过测试步骤

    有时候我们不想再执行maven的package或者install命令时每次都执行test,那么可以在pom.xml里的build->pluginManagement->plugins新增如 ...

  7. 使用Collections类对 集合排序

    对Set<Object>集合进行排序 根据类型的某一个属性去排序 public Set<School> sortByValue(Set<School> set){ ...

  8. ALTERA DDRII IP核使用

    提到DDRII,大家应该都不陌生,DDRII SDRAM是第二代双倍速率同步动态RAM.今天小编给大家介绍一下QUARTUS II 下调用DDRII软核. 新建QUARTUSII工程之后,在tool下 ...

  9. org/apache/hadoop/hbase/mapreduce/TableReducer:Unsupported major.minor version52.0

    问题详情: 问题原因: <dependency>    <groupId>org.apache.hbase</groupId>    <artifactId& ...

  10. proc文件系统详解(原创)

    Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它常见的文件系统不同的是,/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,用户可以通过 ...