ZOJ 3987 Numbers (贪心)

探讨了如何将一个非负整数拆分成指定数量的非负整数之和,并使这些数的位或运算结果最小。通过分析整数的二进制表示,采用贪心策略实现最优拆分。

Description

DreamGrid has a nonnegative integer n. He would like to divide n into m nonnegative integers a1,a2,,am and minimizes their bitwise or (i.e. n=a1+a2++am and a1 OR a2 OR  OR am should be as small as possible).

 

Input

There are multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case:

The first line contains two integers n and m ( 0n<101000,1m<10100 ).

It is guaranteed that the sum of the length of does not exceed 20000 .

 

Output

For each test case, output an integer denoting the minimum value of their bitwise or.

 

Sample Input

5
3 1
3 2
3 3
10000 5
1244 10

 

Sample Output

3
3
1
2000
125

 

题意

将整数 n 拆分为 m 个数的和,输出这 m 个数 or 的最小值。

 

思路

考虑贪心,我们知道,m 个数的二进制中某一位有一个为 1 ,最终的结果这一位一定是 1 ,因此尽可能多的让这 m 个数充满二进制的这一位则为最优。

记录 n 的二进制位数,从高位向低位开始枚举,对于当前位 i ,首先判断 n 是否可以充满 mi 位以下的部分。

  • 若可以,则说明溢出部分会被放置在当前位,于是尽可能多的填充当前第 i 位共 m 个二进制位。
  • 若不可以,则说明剩余的数字可以被安置在低位,当前位为 0

     

    AC 代码

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
    
        private static Scanner cin;
        static BigInteger n, m;
    
        public static void solve() {
            int len = 0;
            BigInteger tmp = n;
            while (tmp.compareTo(BigInteger.ZERO) != 0) {
                tmp = tmp.shiftRight(1);
                len++;
            }
            BigInteger ans = BigInteger.ZERO;
            for (int i = len - 1; i >= 0 && n.compareTo(BigInteger.ZERO) > 0; i--) {
                BigInteger cnt = BigInteger.ONE.shiftLeft(i);
                tmp = cnt.subtract(BigInteger.ONE).multiply(m); // 检验n是否可以填满m个低位
                if (tmp.compareTo(n) < 0) { // 若不可以填满则说明当前位一定被占用
                    n = n.subtract(cnt.multiply(m.min(n.shiftRight(i)))); // 尽可能多的填充当前位
                    ans = ans.or(cnt);
                }
            }
            System.out.println(ans);
        }
    
        public static void main(String[] args) {
            cin = new Scanner(System.in);
            int T;
            T = cin.nextInt();
            while ((T--) > 0) {
                n = cin.nextBigInteger();
                m = cin.nextBigInteger();
                solve();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值