Minimum Sum of Array

新手小白 必须精进的 “ FLASH分区管理Code and Data ” !!!最强文章解析!!! FLASH“代码区”和“数据区”不是每个项目都必须显式管理,但只要你的产品功能越来越丰富,对可靠性和扩展性要求提高,就一定要学会科学划分和使用。这也是嵌入式工程师进阶路上的必修课! 阅读详情

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

 

题意:若数组中的一个数a能被另一个数b整除,则可以用b替换掉a,经过多次操作后,使得该数组的和尽可能的小,并输出这个最小值。

 

思路:在每个数的所有因子中,找到那个在题目所给数组中并且最小的那个因子,用这个因子替换掉这个数。

 

代码实现:map记录每个数出现的次数,用迭代器遍历map,找到每个数那个满足条件的最小因子,(求因子时注意用sqrt优化)并替换之(直接让因子出现的个数加上那个数出现的个数,再将那个数删掉)

 

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 1e6+100
using namespace std;
const int N=10000 + 10 ;
typedef long long  LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };

int GCD(int a,int b)
{
    return b ? GCD(b,a%b) : a;
}
map<LL,LL>::iterator it;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        map<LL,LL>mp;
        stack<LL>p;
        mp.clear();
        LL i,j,n,z=0;
        LL rep,ans=0,flag=0;
        scanf("%lld",&n);
        for(i=0; i<n; i++)
        {
            scanf("%lld",&rep);
            mp[rep]++;
        }
        for(it=mp.begin(); it!=mp.end(); it++)
        {
            LL j=it->first;
            if(j==1)
            {
                flag=1;
                break;
            }
            else if(j>3)
            {
                LL g=0,nflag=0;
                LL ans[N]= {0};
                for(LL i=2; i*i<=j; i++)
                {
                    if(j%i==0)
                    {
                        if(mp.count(i))
                        {
                            mp[i]+=mp[j];
                            it--;
                            mp.erase(j);
                            nflag=1;
                            break;
                        }
                        if(mp.count(j/i))
                        {
                            p.push(j/i);
                            g++;
                        }
                    }
                }
                if(!nflag)
                {
                    if(g!=0)
                    {
                        mp[p.top()]+=mp[j];
                        it--;
                        mp.erase(j);
                    }
                }
            }
        }
        if(flag)
            ans=n;
        else
        {
            for(it=mp.begin(); it!=mp.end(); it++)
            {
                ans+=it->second*it->first;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

寒武纪面试——数字IC,数字逻辑岗 面试过程:面试是电话面试,通话时间大致在40分钟左右!面试体验挺不错的。面试的岗位是数字逻辑工程师。本人双非,拿过一个国一(南京FPGA大赛),一个数学建模省二。 阅读详情

相关推荐

基于STM32F407与AD9910/AD9959的高频可调DDS信号源设计与实战

本文详细介绍了基于STM32F407微控制器与AD9910/AD9959高性能DDS芯片的高频可调信号源设计与实现。文章从DDS原理入手,阐述了双芯片方案的优势,并重点分享了高频电路设计中的电源、PCB布局布线等关键硬件设计技巧与避坑指南。同时,提供了通过SPI配置DDS芯片及利用Matlab生成任意波形的软件驱动方法,旨在帮助开发者打造一个覆盖10Hz至4200MHz的实用信号发生器。

weixin_36184718的博客 122

E. Minimum Array(贪心)

题目链接:https://codeforces.com/problemset/problem/1157/E 题目大意: 首先输入一个n,然后输入a数组,有n个数。然后输入b数组,也有n个数。然后你可以对b数组进行排序,使得(a[i]+b[i])%n从1~n的字典序最小。 具体思路: 通过分析数据范围,我们可以知道两个数相加凑出来n是最合理的,对于当前的数t1,我们要找的是 n-t...

diaomeijiao3430的博客 187

C#实战:通过ZPL指令与斑马打印机交互及状态监控全解析

本文全面解析了在C#中通过ZPL指令与斑马打印机进行交互及状态监控的实战方法。文章详细介绍了TCP/IP网络打印和Windows Raw打印两种核心连接方式,并深入讲解了ZPL指令的构建。重点阐述了通过SGD命令和斑马官方SDK实现实时打印状态监控的策略,为工业自动化场景下的可靠打印提供了完整解决方案。

weixin_29294395的博客 117

Minimum Array

https://codeforces.com/contest/1157/problem/E 题意:重新排序b数组,使得ci=(ai+bi)%n的字典序最小 题解:用multiset存储b数组,lower_bound查询,不要用vector,容易TLE /* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> ...

无限迭代中...... 673

Qt开发中彻底解决中文乱码问题

中文乱码问题主要源于编码不一致需要同时设置控制台和Qt应用程序的编码确保项目文件和环境配置的编码一致文件操作时注意指定正确的编码。

qq_53832058的博客 3404

Ansys Circuit新手必看:IBIS模型导入时,Pin Import和Buffer Import到底怎么选?

本文详细解析了Ansys Circuit中IBIS模型导入时Pin Import与Buffer Import的选择策略,帮助SI/PI工程师根据仿真需求做出正确决策。通过对比两种方式的特性差异和应用场景,提供实用的选择指南和性能优化建议,提升信号完整性仿真效率。

weixin_26846533的博客 261

C.Minimum Array(二分+set)

题目描述: 知识点: lower_bound和uper_bound lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值第一个出现的位置。 upper_bound(起始地址,结束地址,要查找的数值) 返回的是数值最后一个出现的位置。 binary_search(起始地址,结束地址,要查找的数值) 返回的是是否存在这么一个数,是一个bool值...

b10221001的博客 251

Vue3 - H5实现省市区三四级联动组件,选择地址自定义省市区三级联动插件,弹框式多级联动选择器区域选择,vue3实现省市区三级联动地址选择组件(支持接口数据懒加载,样式精美漂亮,附带详细示例源码)

vue3实现省市区三级联动地址选择组件(附address文件),vue3实现省市区三级联动, 实现省、市、县地区三级联动功能,vue3省市区选择器,VUE3 h5如何实现省市区街道的四级联动选择,vue实现省市区选择联动,vue3城市三级联动(自定义组件)_VUE三级联动,插件省市区联动,vue如何实现省市区选择器,vue3怎样实现省市区地址三级联动,vue四级联动,vue3级联选择器,vue3实现选择省市区镇(街道)四级级联选择,包含三级级联选择,省市区街道三级四级联动选择器,VUE3自定义多列选择器省市

🏆 前端领域优质创作者 3068

E. Minimum Array(树状数组+二分)

其实贪心还是比较显然其实贪心还是比较显然其实贪心还是比较显然 对于一个ai,我们最好找到bj=n−ai对于一个a_i,我们最好找到b_j=n-a_i对于一个ai​,我们最好找到bj​=n−ai​ 这样对n求余后就是0,最优这样对n求余后就是0,最优这样对n求余后就是0,最优 如过没有n−ai这个数,就一个一个往后看直到n如过没有n-a_i这个数,就一个一个往后看直到n如过没有n−ai​这个数,就一个一个往后看直到n 如果还没有,再从0开始看到n−ai−1如果还没有,再从0开始看到n-a_i-1如果还没有,再

jziwjxjd的博客 645

TC3XX - MCAL知识点(十一):WDG EB-tresos配置说明与代码浅析

在AUTOSAR中,看门狗栈实现了三种监控机制:实时监控:用于周期软件的定时监控期限监督:针对非周期软件逻辑监督:用于监督执行顺序的正确性看门狗(WDG)驱动实现AUTOSAR(4.2.2)中规定的看门狗SWS,提供初始化、改变操作模式和设置触发条件(超时)等服务,供看门狗硬件(WDT)用户使用。WDG驱动的用户包括:模式管理模块(用于初始化),WdgM通过Wdglf设置触发条件和驱动运行模式设置。WdgM实施上述监督机制。AUTOSAR扩展触发概念,以支持窗口看门狗模式。

梦想技术家 3603

芒果YOLOv8改进16:检测头Head篇:即插即用|新颖轻量化非对称多级压缩LADH检测头,原创改进适配YOLOv8高效检测头,检测头新颖性改进,全网首发独一份

YOLOv8代码改进教程:即插即用|新颖轻量化非对称多级压缩LADH检测头🎈

包括YOLOv5、YOLOv7、YOLOv8等模型改进 5611

Minimum Sum of Array (因数倍数 数据处理)

原题:Minimum Sum of Array 题意: 有n个数,可以把大的数变成小的只要小的数也存在并且大数是小数的倍数 问任意改变后sum的最小值 解析: 对于这一堆数,首先先排序,然后的处理办法有很多 一: 从前往后扫,对扫到的每一个数再往后扫,时间当然是不够的啦 二: 既然是倍数,我们可以针对每个数,排掉它的倍数,比如2的时候我们就排掉4,6 这样为什么可以节省时间呢...

QAQ 638

假期搞定!2026国社科申报AI全攻略指南:从选题到创新,7大核心要点手把手教你!

最后给大家推荐的400多所高校教师都在用的AI平台——高级学术版专业应用平台,集成了国际主流学术应用,以及ChatGPT、Claude、Gemini、Grok等Al大模型的高级版本平台,还有,可以辅助大家高效完成科研和学术写作任务,当然撰写硕博论文,课题、职称论文、国社科、国自然基金本子也是手拿把掐。你的任务是:用高度凝练的方式说明在(某一明确国家战略 / 政策方向,如:中国式现代化、共同富裕、基层治理现代化、数字中国、文化强国、国家安全等)背景下,我究竟要研究一个什么“具体而可操作的问题”。

智能AI让学术科研更容易 624

F - Minimum Sum of Array

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 di...

xizi_ghq的博客 514

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Minimum Sum of Array(超时)

题意: n个数,若b能整除a,就把a变成b,最后求出这n个数的最小和。 开始的思路:先排序,再从小的向后遍历,遇到它的倍数,sum就加上这个数,然后将这个数变成0,后面遍历的时候就不处理这个数了。改了很多发,可能因为sort排序的时间复杂度为O(nlogn),一直超时。 ac的思路:因为每个数最大为1e6,所以直接用数组存每个数出现的次数就OK。 超时代码 #include<bit...

weixin_44019380的博客 232

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Gym - 101810F Minimum Sum of Array

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 di...

yz467796454的博客 796

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

F - Minimum Sum of Array(素数筛)

F - Minimum Sum of Array(素数筛) You are given an arrayaconsisting ofnintegersa1, ..., an. In one operation, you can choose2elementsaiandajin whichaiis divisible byajand transformaito...

石旭先森的博客 260

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 ...

weixin_30335353的博客 109

CodeForces_Minimum Sum of Array(思路)

题目链接:F. Minimum Sum of Array 题目: 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 ...

沉默的大多数 376

Gym - 101810F Minimum Sum of Array

这题我自己的代码在第二组数据上死都过不去,不知道为什么,现在还是没有找到错误案例,希望有大佬帮我测试一下哪里有错。。。。 我自己的错误代码: //F #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;algorithm&gt; using namespace std; typedef long long LL; #def...

fx714848657的博客 272

LeetCode2834. Find the Minimum Possible Sum of a Beautiful Array

【代码】LeetCode2834. Find the Minimum Possible Sum of a Beautiful Array。

叶卡捷琳堡的博客 1314

F - Minimum Sum of Array Gym - 101810F (素数筛用法)

You are given an arrayaconsisting ofnintegersa1, ..., an. In one operation, you can choose2elementsaiandajin whichaiis divisible byajand transformaitoaj. A numberxis said to be di...

qq_44134712的博客 209

Leetcode 3117. Minimum Sum of Values by Dividing Array

Leetcode 3117. Minimum Sum of Values by Dividing Array

codename_cys的博客 371

Minimum Sum of Array(map迭代器)

codeforces 910c 转自两位大佬:http://www.cnblogs.com/wkfvawl/p/9387634.html https://i.cnblogs.com/EditPosts.aspx?opt=1 You are given an array a consisting of n integers a1, ..., an. In one operati...

weixin_30498807的博客 94

LeetCode C++ 1085. Sum of Digits in the Minimum Number【Array】简单

Given an array A of positive integers, let S be the sum of the digits of the minimal element of A. Return 0 if S is odd, otherwise return 1 . Example 1: Input: [34,23,1,24,75,33,54,8] Output: 0 Explanation: The minimal element is 1, and the sum of those d

memcpy0的博客 421

LeetCode | 977.Squares of a Sorted Array, 209. Minimum Size Subarray Sum, 59. Spiral Matrix II

Link: https://leetcode.com/problems/squares-of-a-sorted-array/Given an integer array sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. 209. Minimum Size Subarray Sum Link: https://leetcode.com/pr

weixin_43615320的博客 267
上一篇: Equalize the Remainders
下一篇: 树状数组 (三种模型)
codertcm
博客等级 码龄8年 80粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值