1. 程式人生 > >Gym - 101911C Bacteria (規律題)

Gym - 101911C Bacteria (規律題)

傳送門:點我

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 

7">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 n(1n2105)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai109)(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 = 0 ; i < n ; i++){
        LL x;
        scanf("%lld",&x);
        q.push(x);
    }
    LL ans = 0;
    while(q.size()>=2){
        LL f1 = q.top();q.pop();
        LL f2 = q.top();q.pop();
        if(f2%f1 != 0)return puts("-1"),0;
        LL d = f2/f1;
        if((d&(d-1))!=0) return puts("-1"),0;
        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
*/