ZOJ 3987 Numbers (贪心)
来源:互联网 发布:网络的逻辑结构 编辑:程序博客网 时间:2024/06/05 14:30
Description
DreamGrid has a nonnegative integer
n . He would like to dividen intom nonnegative integersa1,a2,…,am and minimizes their bitwise or (i.e.n=a1+a2+⋯+am anda1 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 andm (0≤n<101000,1≤m<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
53 13 23 310000 51244 10
Sample Output
3312000125
题意
将整数
思路
考虑贪心,我们知道,
记录
- 若可以,则说明溢出部分会被放置在当前位,于是尽可能多的填充当前第
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(); } }}
- ZOJ 3987 Numbers (贪心)
- ZOJ 3987 && 2017CCPC秦皇岛 G:Numbers(高精度+贪心)
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
- ZOJ 3987 Numbers CCPC2017 Qinhuangdao(贪心+Java大整数)
- ZOJ 3987 Numbers(枚举)
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制乱搞、贪心)
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制、贪心)
- ZOJ 3987 Numbers 2017CCPC秦皇岛站G题 大整数 二进制 贪心
- ZOJ 2343(贪心)
- ZOJ 3715 (贪心)
- zoj-3721(贪心)
- Persistent Numbers(贪心+高精度)
- zoj3987—Numbers(贪心)
- ZOJ _3607_Lazier Salesgirl(贪心)
- ZOJ 2921 Stock(贪心)
- zoj 2921 Stock(贪心)
- ZOJ 1877 Bridge(贪心)
- ZOJ 3953 Intervals (贪心)
- 如何优化网站加载速度
- Feasibility of Learning & Training versus Test(林轩田-机器学习)
- Python学习笔记
- IDEA Properties中文unicode转码问题
- 解决lxml安装时“lxml-4.1.0-cp27-cp27m-win32.whl is not a support wheel on this platform”报错
- ZOJ 3987 Numbers (贪心)
- CSS 解決版面撑开、英文断字、避头尾、段落左右边界不齐等方法
- 2017.11.1~11.2模拟题
- SpringCloud教程 | 第五篇: 路由网关(zuul)
- 请求转发和重定向的区别
- Guitar Pro 的双十一特惠活动,正在如火如荼进行中...
- 第七课:scroll 的抽象与应用与懒加载与Loading【推荐歌单】
- 基于vue-cli快速构建
- MapReduce的理解