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这一容器来去重。对容器中的每一个元素遍历,开始我的打算是将每一个元素的因子都拆分,再按照从小到大的顺序来一一枚举因子(因为如果容器中有元素等于交小的因子
替换后得到的答案就是最小的),可惜超时了,于是寻求另一种方法来解决。我同学给我提供了一个新的思路,我们在求某一个数的因子的时候,为了优化算法,降低时间复杂度,会采用一种方法,比如求
12的因子的时候,我们从1到12逐个遍历,知道2是12的因子,那么也能得到12/2=6也是12的因子;知道了3是12的因子,那么也同时得到了4也是12的因子。这里也是一样的,我们也是先依次去从小的因子
出发,找小的因子的过程中也找到了其对应的比其大的因子。如果小因子不存在,再看看对应的较大的那个因子是否存在,存在的话就保存下来。之后也是这样增大因子,重复这个过程,要是存在对应的交大的因子就更新保存的结果,这些较小的因子都找过了依旧不满足条件,那么就找那个保存的那个较大的因子。
还需要注意的是mp.erase(),当在迭代器中调用这个函数的话,需要先返回上一个迭代器,不然指针会变为一个野指针(可参考链表理解)。
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define LL long long int
using namespace std;
int main()
{
int t,flag,flag1;
LL i,j,a,n,k;
LL ans,x;
map<LL,LL>mp;
map<LL,LL>::iterator it;
scanf("%d",&t);
while(t--)
{
mp.clear();
scanf("%lld",&n);
for(i=;i<n;i++)
{
scanf("%lld",&a);
mp[a]++;
}
flag=;
ans=;
for(it=mp.begin();it!=mp.end();it++)
{
k=it->first;
if(k==)///出现1
{
flag=;
break;
}
flag1=;
x=;
for(i=;i*i<=k;i++)
{
if(k%i==)
{
if(mp.count(i))
{
mp[i]+=mp[k];
it--;
mp.erase(k);
flag1=;
break;
}
else if(mp.count(k/i))
{
x=k/i;
}
}
}
if(!flag1)
{
if(x)
{
mp[x]+=mp[k];
it--;
mp.erase(k);
}
}
}
if(flag)
{
printf("%lld\n",n);
}
else
{
for(it=mp.begin();it!=mp.end();it++)
{
ans+=it->second*it->first;
}
printf("%lld\n",ans);
}
}
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. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  3. JavaScript Array map() 方法

    语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值index:可选.当期元素的索引值ar ...

  4. 数学 - Whu 1603 - Minimum Sum

    Minimum Sum Problem's Link ------------------------------------------------------------------------- ...

  5. 数组的方法 Array.map();Array.every()和Array.some();数组的indexof();检测是否是数组isArray(obj);

    数组的方法 Array.map(); 栗子: var a=[1,2,,3]; var b=a.map( function(value){return value*value} ); alert(b); ...

  6. Minimum Sum(思维)

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

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

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

  8. 兼容低版本JS的Array.map方法

    前几天去别的公司面试遇到个这样的问题,兼容IE7下的Array.map方法,一脸蒙蔽.后面回来查了下资料发现.Array.map方法是ECMA-262 标准中新添加的方法,在低版本的JS中是木有的. ...

  9. Array.from();Object.keys();Array.map()

    Array.from():方法从一个类似数组或可迭代对象创建一个新的数组形式: const bar = ["a", "b", "c"]; A ...

随机推荐

  1. OSI参考模型和TCP/IP模型基本知识

    OSI七层模型 为了解决网络之间的兼容性问题,实现网络设备间的相互通信,ISO于1984年提出的OSI参考模型(开放系统互连参考模型).但是由于种种原因,并没有一种完全忠实于OSI参考模型的协议族流行 ...

  2. 偏前端-vue.js学习之路初级(一)概念

    首先--不推荐新手直接使用 vue-cli,尤其是在你还不熟悉基于 Node.js 的构建工具时.    新建一个html,引入一下js: <!-- 开发环境版本,包含了有帮助的命令行警告 -- ...

  3. crontab基础笔记 思维导图版

    直接上图吧----------------------------------------------------------------------------------------------- ...

  4. 如何通过SQL语句写入webshell

    在web应用场景下,经常会碰到SQL注入场景,如页面能够执行SQL语句,那么可能会有直接通过SQL语句写入webshell的风险,常见的phpmyadmin环境下,通过几个语句可以轻松将一句话木马写入 ...

  5. golang基础--Gocurrency并发

    Go并发特点 goroutine只是由官方实现的超级"线程池"而已,每个实例4-5kb的栈内存占用和用于实现机制而大幅减少的创建和销毁开销. 并发不是并行(多CPU): Concu ...

  6. kaggle之员工离职分析

    本文探讨的是kaggle中的一个案例-员工离职分析,从数据集中分析员工的离职原因,并发现其中的问题.数据主要包括影响员工离职的各种因素(工资.绩效.工作满意度.参加项目数.工作时长.是否升职.等)以及 ...

  7. 20+ Docs and Guides for Front-end Developers (No. 5)

    It’s that time again to choose the tool or technology that we want to brush up on. If you feel like ...

  8. 20145226 《Java程序设计》第3周学习总结

    教材学习内容总结 学习目标 区分基本类型与类类型 理解对象的生成与引用的关系 掌握String类和数组 理解封装的概念 掌握构造方法的定义 理解重载的概念 掌握static的应用 教材第四章内容总结 ...

  9. 20145234黄斐《java程序设计》第二周

    教材学习内容总结 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Type),其中类类型也叫参考类型(Reference Type). 字节类型,也叫byte类型, ...

  10. 20145234黄斐《Java程序设计》实验二—Java面向对象程序设计

    1.提交最后三个测试用例都通过的截图,截图上要有画图加水印,输入自己的学号. 2. 以 TDD的方式研究学习StringBuffer 3.对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步 ...