传送门:点我

Time limit2000 ms

Memory limit262144 kB

Recently Monocarp has created his own mini-laboratory!

The laboratory contains nn bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 77 merge, one bacterium with size 1414 is the result.

It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store.

You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Input

The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), where aiai is the size of the ii-th bacterium in the laboratory.

Output

If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1.

Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Examples

Input
2
1 4
Output
2
Input
3
3 6 9
Output
-1
Input
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
1

Note

In the first example Monocarp should buy one bacterium having size 11 and one bacterium having size 22. Then Monocarp will have 44 bacteria having sizes [1,4,1,2][1,4,1,2]. Then two bacteria having sizes 11 can be merged into one having size 22. Then Monocarp will have 33 bacteria having sizes [2,4,2][2,4,2]. Then two bacteria having sizes 22 can be merged into one having size 44. Then Monocarp will have 22 bacteria having sizes [4,4][4,4], which can be merged into one having size 88.

In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria.

In the third example Monocarp needs to buy one bacterium having size 1000000000.

题意:给N个数,只有相同的两个数才能结合起来,加在一起。比如说2只有和2结合,成为4。

比如样例1的 1 4 ,我们需要加一个1,和原来的1结合,成为2,然后因为2没有可以组合的,所以再加一个2,组合成4,4和原来的4组合成为8。

询问的就是能不能通过添加一些数,最后能组合成1个数。如果不能输出-1,如果能输出还缺少的数的个数。

思路:

简单推理一下会发现,从小到大取,每2个数,后一个如果不是前一个的倍数,肯定不行。后一个除以前一个不是2次幂,也不行。

然后就是简单的推上去合并的问题了。这个过程用队列模拟一下。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
using namespace std;
int main()
{
priority_queue<LL,vector<LL>,greater<LL> >q;
int n;
scanf("%d",&n);
for(int i = ; i < n ; i++){
LL x;
scanf("%lld",&x);
q.push(x);
}
LL ans = ;
while(q.size()>=){
LL f1 = q.top();q.pop();
LL f2 = q.top();q.pop();
if(f2%f1 != )return puts("-1"),;
LL d = f2/f1;
if((d&(d-))!=) return puts("-1"),;
q.push(f2*2LL);
ans += (LL)log2(f2*1.0/f1);
}
printf("%lld\n",ans);
}
/*
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 3
3 6 9 2
1 4
*/

Gym - 101911C Bacteria (规律题)的更多相关文章

  1. Bacteria (Gym - 101911C)

    2018-2019 ACM-ICPC, NEERC, Southern Subregional Contest, Qualification Stage Bacteria Gym - 101911C ...

  2. LightOJ1010---Knights in Chessboard (规律题)

    Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...

  3. Codeforces - 规律题 [占坑]

    发现自己容易被卡水题,需要强行苟一下规律题 CF上并没有对应的tag,所以本题集大部分对应百毒搜索按顺序刷 本题集侧重于找规律的过程(不然做这些垃圾题有什么用) Codeforces - 1008C ...

  4. http://codeforces.com/gym/100623/attachments E题

    http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...

  5. ACM_送气球(规律题)

    送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...

  6. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. http://codeforces.com/gym/100623/attachments H题

    http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...

  8. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  9. 洛谷 P1876 开灯(思维,枚举,规律题)

    P1876 开灯 题目背景 该题的题目是不是感到很眼熟呢? 事实上,如果你懂的方法,该题的代码简直不能再短. 但是如果你不懂得呢?那...(自己去想) 题目描述 首先所有的灯都是关的(注意是关!),编 ...

随机推荐

  1. <基础> PHP 进阶之 抽象类(abstract)、接口(interface)、Trait(特征)

    抽象类 PHP 5 支持抽象类和抽象方法.定义为抽象的类不能被实例化. 抽象方法只能在抽象类中,抽象类中可以包含非抽象方法 被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现 继 ...

  2. 【Source Insight 】之marco学习笔记2

    现在我们看先看一个 官方地址https://www.sourceinsight.com/download/macro-files/中的 autoexp.em Automatically expands ...

  3. 前后台交互实现点击超链接通过指定的 url 去网络或者文件服务器下载文件

    前台 VUE 界面: <el-table-column prop="attachment" align="center" label="附件详情 ...

  4. UI5-学习篇-8-本地SAP WEB IDE开发

    1.本地SAP WEB IDE下载 UI5-学习篇-3-Local SAP WEB IDE下载 2.启动Orion服务 解压SAP WEB IDE文件后,双击Orion应用程序启动服务,如下图: 服务 ...

  5. mybatisGenerator自动生成pojo、dao、xml文件

    一.简介: mybatisGenerator是一款自动生成文件工具.本文使用idea2017.3.1来进行操作. 二.文件生成之后的项目结构: 三.开始生成步骤: 1.使用idea生成maven的结构 ...

  6. UNITY2018开启deepprofiling

    ADB方式调试游戏步骤 前提: 1,手机开启 [开发者模式][USB调试] 2,数据线连接手机和电脑 3,安装adb(注意adb版本不对可能导致adb deveices找不到设备,那就换个adb版本) ...

  7. vscode 不显示指定后缀名pyc文件

    不显示python生成的pyc文件 不显示java eclipse编辑器生成的.metadata生成的文件夹 py文件执行后会生成.pyc文件,会影响侧边栏的使用,可以通过如下设置隐藏.pyc等中间文 ...

  8. js 高效拼接字符串

    <script>//如果我们大量使用+=进行字符串拼接的话,将会使界面失去响应(卡死状态) //高效拼接字符串 var StringBuilder=function() { this.da ...

  9. [重点]delphi删除部分字符串(不区分大小写)

    type TDelFlags = set of (dfDelBefore, dfDelAfter); //删除ms字符串中endstr子字符串前面或后面的部分字符串 procedure Delstr( ...

  10. spring使用中ModelAttribute的内容被覆盖

    在前台以get方式向后台提交数据: 后台接收: 后台接收参数的时候,由于user里面也有一个属性为id,后台在接收参数的时候,User里面的id会被重新赋值,这是一个大坑.如果后续继续用User来做操 ...