2015弱校联萌十一大决战之背水一战 D. Divide 二进制思维题

http://www.bnuoj.com/v3/contest_show.php?cid=6869#problem/D

Alice and Bob has found a island of treasure in byteland! They find N kinds of treasures on the island, and each kind of treasure has a certain number, and as in byteland, the value of each treasure will be a power of 2, such as 1,2,4,8 ...

Now the only problem is how to divide treasures fairly, they need to divide the treasures into two parts, and the value of each part is the sum of all treasures' in this part, they want to make the difference between the value of two parts as small as possible, can you help them?

Input


First line of the input is a single integer T(1 <= T <= 20), indicating there are T test cases.

For each test case, the first line contain one integer N(2 <= N <= 10^5), indicate the different kinds of treasures.

Then N line followed, each line will have follow two integer ai(0 <= ai <= 10^5) and xi(0 <= xi <= 10^9), indicate there are xi i-th treasures, and the value of each one is 2^ai.

Output

For each case, you should output a single line, first output "Case #t: ", where t indicating the case number between 1 and T, then a string with only '0' and '1' followed, indicate the minimum difference in binary representation, find more details in samples.

Sample Input

3
2
0 2
2 1
4
0 1
1 1
2 1
3 1
4
0 2
1 1
2 1
3 1

Sample Output

Case #1: 10
Case #2: 1
Case #3: 0

/**
2015弱校联萌十一大决战之背水一战  D. Divide  二进制思维题
题目大意:给定写物品每个有2^a[i]重,每种x[i]个,问将二者平均分成两份,二者的最小差的二进制表示
解题思路:数据如果小点,就是一个背包的问题了。首先从低位往高位处理,jin[i]表示第i位有没有前一位的进位,num[i]表示
          进位后i位置的数,我们找到最前一个该位为1,且并无前一位进位的j,那么2^j,为一份,其前面的所有为一份,做差
          就可以了。因为如果该位为1但有进位的话,完全可以看出改位为0,前一位为2,则可以分成两份的。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
int n,a[maxn],jin[maxn];
LL num[maxn],x[maxn];
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int maxx=-1;
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)scanf("%d%lld",&a[i],&x[i]),maxx=max(maxx,a[i]),num[a[i]]+=x[i];
//        printf(">>>before");
//        for(int i=0;i<=maxx;i++)printf("%lld",num[i]);
//        printf("\n");
        memset(jin,0,sizeof(jin));
        for(int i=0;i<=maxx;i++)
        {
            if(num[i]/2)jin[i+1]=1;
            num[i+1]+=num[i]/2;
            num[i]%=2;
        }
//        printf(">>>");
//        for(int i=0;i<=maxx;i++)printf("%lld",num[i]);
//        printf("\n");
        bool flag=0;
        for(int i=maxx;i>=0;i--)
        {
            if(num[i]&&jin[i]==0)
            {
                maxx=i;
                flag=1;
                break;
            }
        }
        printf("Case #%d: ",++tt);
        if(flag==0)
        {
            puts("0");
            continue;
        }
        int i;
        for(i=0;i<maxx;i++)
        {
            if(num[i]==1)
            {
                num[i]=1;
                for(int j=i+1;j<maxx;j++)num[j]^=1;
                break;
            }
        }
        num[maxx]=(i==maxx);
        for(int i=maxx;i>=0;i--)
        {
            if(num[i]==1)
            {
                for(int j=i;j>=0;j--)printf("%lld",num[j]);
                break;
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BigDecimal.divide()是Java中BigDecimal类提供的一个方法,用于对两个BigDecimal类型的数进行除法计算。它有多种重载形式,可以设置精度、舍入模式等参数。 例如,下面的代码演示了如何使用BigDecimal.divide()方法进行除法计算: ``` import java.math.BigDecimal; public class Main { public static void main(String[] args) { BigDecimal dividend = new BigDecimal("10"); BigDecimal divisor = new BigDecimal("3"); // 使用默认精度和舍入模式进行除法运算 BigDecimal result1 = dividend.divide(divisor); System.out.println(result1); // 输出:3 // 设置精度为2,使用默认舍入模式进行除法运算 BigDecimal result2 = dividend.divide(divisor, 2); System.out.println(result2); // 输出:3.33 // 设置精度为2,舍入模式为向下取整进行除法运算 BigDecimal result3 = dividend.divide(divisor, 2, BigDecimal.ROUND_DOWN); System.out.println(result3); // 输出:3.33 } } ``` 在上面的代码中,我们首先创建了两个BigDecimal类型的数dividend和divisor,然后使用BigDecimal.divide()方法进行除法计算。在第一个示例中,我们使用了默认精度和舍入模式进行除法运算,得到了整数3;在第二个示例中,我们设置精度为2,使用默认舍入模式进行除法运算,得到了保留两位小数的小数3.33;在第三个示例中,我们设置精度为2,舍入模式为向下取整进行除法运算,得到的结果仍然是保留两位小数的小数3.33。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值