CF806C:Prairie Partition(二分)

C. Prairie Partition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k ≥ 00 < r ≤ 2k. Let's call that representation prairie partition of x.

For example, the prairie partitions of 12177 and 1 are:

12 = 1 + 2 + 4 + 5,

17 = 1 + 2 + 4 + 8 + 2,

7 = 1 + 2 + 4,

1 = 1.

Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of numbers given from Alice to Borys.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1012a1 ≤ a2 ≤ ... ≤ an) — the numbers given from Alice to Borys.

Output

Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.

If there are no such values of m, output a single integer -1.

Examples
input
8
1 1 2 2 3 4 5 8
output
2 
input
6
1 1 1 2 2 2
output
2 3 
input
5
1 2 4 4 4
output
-1
Note

In the first example, Alice could get the input sequence from [6, 20] as the original sequence.

In the second example, Alice's original sequence could be either [4, 5] or [3, 3, 3].


题意:给出一个序列,问这个序列可以由多少个数分解而来,分解方式如题。

思路:分解数ans的上限为1的数目,加如可以分解为x组,其中x<count[1],那么[x,count[1]]的都可以作为答案,因为将1提取出来又是新的一组。那么可以二分出可分解的最小组数。

# include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int maxn = 1e5+3;
int a[maxn]={0}, b[maxn]={0};//a[i]为2^i个数,b[i]为2^i~2^i+1个数。

bool judge(int x)
{
    int t = a[0]-x+b[0];//非法数字。
    for(int i=1; i<50; ++i)
    {
        if(a[i] <= x)//表示这里要断开了。
        {
            t -= min(t, x-a[i]);//捎上非法数字再断开。
            x = a[i];//有效组数减少为a[i]。
        }
        else
            t += a[i]-x;
        t += b[i];
    }
    return t<=x;
}
int main()
{
    int n;
    LL m;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d",&m);
        int c=0;
        bool f = true;
        while(m>1)
        {
            if(m&1) f = false;
            m>>=1;
            ++c;
        }
        if(!f) ++b[c];
        else ++a[c];
    }
    int l=ceil(a[0]*1.0/2), r=a[0];
    while(l<r)
    {
        int mid = l+r>>1;
        if(judge(mid)) r=mid;
        else l = mid+1;
    }
    if(!judge(r)) return 0*puts("-1");
    else
        for(int i=r; i<=a[0]; ++i) printf("%d ",i);
    puts("");
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值