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 ...
随机推荐
- scrapy 碎片
1.启动命令 2.目录结构 3.文件说明 4.架构图示 5.代码流程 参考资料: http://www.cnblogs.com/yangxt90/articles/9021530.html http: ...
- linux下json库的编译及例程
.下载JsonCpp http://sourceforge.net/projects/jsoncpp/files/ .下载scons http://sourceforge.net/projects/s ...
- 神经网络中的Softmax激活函数
Softmax回归模型是logistic回归模型在多分类问题上的推广,适用于多分类问题中,且类别之间互斥的场合. Softmax将多个神经元的输出,映射到(0,1)区间内,可以看成是当前输出是属于各个 ...
- 高级C/C++编译技术之读书笔记(三)之动态库设计
最近有幸阅读了<高级C/C++编译技术>深受启发,该书深入浅出地讲解了构建过程(编译.链接)中的各种细节,从多个角度展示了程序与库文件或代码的集成方法,提出了面向代码复用和系统集成的软件架 ...
- 《DSP using MATLAB》示例Example7.18
代码: M = 33; alpha = (M-1)/2; l = 0:M-1; wl = (2*pi/M)*l; T1 = 0.1095; T2 = 0.598; Hrs = [zeros(1,11) ...
- 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
[spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...
- loj #6138. 「2017 山东三轮集训 Day4」Right
题目: 题解: 暴力一波 \(SG\) 函数可以发现这么一个规律: \(p\) 为奇数的时候 : \(SG(n) = n \% 2\) \(p\) 为偶数的时候 : \(SG(n) = n \% (p ...
- Tornado服务端基本的配置文件(Python)
web_set = { # -------一般设置-------- 'debug': True, 'port': 8836, 'autoreload': True, 'ui_modules': {}, ...
- jquery调用spring mvc接口返回字符串匹配
背景:有个增删改页面,用jquery祭出ajax异步调用接口,spring mvc响应对象是个json字符串,jquery根据响应结果判断,如果删除成功给出提示.那么问题来了,接口里响应的字符串怎么匹 ...
- 关于OPC自动化接口编程(OPCDAAuto.dll)几点注意问题
为了能够在工作中方便的应用OPC和充分的理解OPC的开发流程.内部机制,这两天正在研究开发OPC客户端程序,一般我们开发OPC客户端程序有以下几种方式: (1) 使用OPCNetAPI,需 ...