Minimum Sum of Array(map)
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?
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.
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.
1
5
2 2 3 6 6
#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)的更多相关文章
- 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 ...
- 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 ...
- 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...
- HDU 3473 Minimum Sum(划分树)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- JS中集合对象(Array、Map、Set)及类数组对象的使用与对比(转载)
在使用js编程的时候,常常会用到集合对象,集合对象其实是一种泛型,在js中没有明确的规定其内元素的类型,但在强类型语言譬如Java中泛型强制要求指定类型. ES6引入了iterable类型,Array ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- Minimum Sum(思维)
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 563 Accepted ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
随机推荐
- react style: 二级菜单
1.样式 @import "../../styles/varibles"; .app-sidebar { overflow: hidden; width: 180px; > ...
- Agilent RF fundamentals (11)-Vector modulator
Vector modulator 矢量调制器:调整信号的幅度和相位 http://www.21ic.com/app/test/201805/762401.htm
- slab机制总结篇
一: slab是为了解决内部碎片提出的,还是外部碎片? 为了解决内部碎片. 内部碎片的产生:因为所有的内存分配必须起始于可被 4.8 或 16 整除(视处理器体系结构而定)的地址或者因为MMU的分页机 ...
- HAWQ取代传统数仓实践(十五)——事实表技术之无事实的事实表
一.无事实事实表简介 在多维数据仓库建模中,有一种事实表叫做"无事实的事实表".普通事实表中,通常会保存若干维度外键和多个数字型度量,度量是事实表的关键所在.然而在无事实的事实表中 ...
- VGGNet网络结构
深度神经网络一般由卷积部分和全连接部分构成.卷积部分一般包含卷积(可以有多个不同尺寸的核级联组成).池化.Dropout等,其中Dropout层必须放在池化之后.全连接部分一般最多包含2到3个全连接, ...
- Python3 pyinotify 监视文件、文件夹修改
/************************************************************************************ * Python3 pyin ...
- 【retina】手机上 1PX 边框
1像素边框,在2倍屏幕上为2“占位”,3倍屏上为3“占位”,但设计师就要1“占位” .content h1:after, .content h2:after { border-top: 1px sol ...
- CSS命名规范参考及书写注意事项
CSS书写顺序 *{ /*显示属性*/ display position float clear cursor … /*盒模型*/ margin padding width height /*排版*/ ...
- Mybatis自动生成实体类、dao接口和mapping映射文件
由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类.dao接口以及它的映射文件,然后直接拷贝到 ...
- Linux环境变量从用户配置改为系统配置
部署了一个新的tomcat到一个新的用户下,发下启动失败了 /home/personal/apache-tomcat/bin/catalina.sh: line 434: /usr/lib/jvm/j ...