Numbers


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.a1+a2+...+am=n  and a1 OR a2 OR a3...OR am should be as small as possible).

Input

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

The first line contains two integers n  and  m ( 0<=n<=1e1000 1<=m<=1e100).

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

Author: LIN, Xi

Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site

/*
* 题意:给你一个1e1000的数,让你分成最多1e100位,然后互相进行或运算,得到的值最小
*
* 思路:让高位最小然后向低位贪心,判断当前为能否为0,能为0的条件是,后面几位都是1,并且有m个,加起来如果大于当前值的话
* 那么这位就可以为0,否则只能为1,既然为1了,那么尽量多填1,这样保证了结果最优
* */
import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String[] args) {
// write your code here
Scanner in=new Scanner(System.in);
int t;
BigInteger n,m,x,sum;
BigInteger [] pre=new BigInteger[];//表示后缀和
String s;
x=new BigInteger("");
pre[]=new BigInteger("");
for(int i=;i<;i++){
pre[i]=pre[i-].add(x);
x=x.multiply(new BigInteger(""));
}
t=in.nextInt();
for(int ca=;ca<t;ca++){
n=in.nextBigInteger();
m=in.nextBigInteger();
sum=n;
int tol=;
while(n.compareTo(new BigInteger(""))==){
tol++;
n=n.divide(new BigInteger(""));
}
x=new BigInteger("");
//判断这一位的累加和能不能用后边的所有空间放下,能的话就转移到下一位,不能的话就把这一位都填满
for(int i=tol;i>=;i--){//枚举每位
x=x.multiply(new BigInteger(""));
int ok=sum.compareTo(pre[i - ].multiply(m));
if ( ok<=) {//能用剩余的空间放下
continue;
} else {//放不下
x = x.add(new BigInteger(""));
BigInteger cur = pre[i].subtract(pre[i - ]);
BigInteger cnt = sum.divide(cur);
if (cnt.compareTo(m) >= ) {
sum = sum.subtract(cur.multiply(m));
} else {
sum = sum.subtract(cur.multiply(cnt));
}
}
}
System.out.println(x);
}
}
}

2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)的更多相关文章

  1. 2017CCPC秦皇岛 G题Numbers&&ZOJ3987【大数】

    题意: 给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小. 思路: 从二进制的角度分析,如果这m个数中有一个数某一位为1,那么最后或起来这一位肯定是为1的, ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  3. ZOJ 3981 && 2017CCPC秦皇岛 A:Balloon Robot(思维题)

    A - Balloon Robot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Sub ...

  4. ZOJ - 3987 - Numbers (大数 + 贪心)

    参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...

  5. G - 密码 kmp、贪心、manachar等等等等

    G - 密码 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit Status ...

  6. BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...

  7. zoj 1025Wooden Sticks(贪心)

    递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  8. 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988

    题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...

  9. 2017CCPC秦皇岛 A题Balloon Robot&&ZOJ3981【模拟】

    题意: 一个机器人在长为M的圆形轨道上送气球,当机器人到达M号点的时候下一站会回到1号点,且全程不会停止运动.现在在长为M的轨道上有N个队伍,队伍会在某个时间做需要一个气球,机器人需要送过去.一共有P ...

随机推荐

  1. 【设计模式】module(模块)模式

    写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...

  2. hdu4632

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  3. 1001 数组中和等于K的数对 1002 数塔取数问题 1003 阶乘后面0的数量 1004 n^n的末位数字 1009 数字1的数量

    1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...

  4. qplot函数用法(转载)

    http://blog.csdn.net/u014801157/article/details/24372499 写的很全面 放在这里记录下

  5. Hibernate Mapping Exception:-9

    if("true".equals(map.get("isAudited"))){ isAudited="=";//已审核 }else{ is ...

  6. 通过npm写一个cli命令行工具

    前言 如果你想写一个npm插件,如果你想通过命令行来简化自己的操作,如果你也是个懒惰的人,那么这篇文章值得一看. po主的上一篇文章介绍了定制自己的模版,但这样po主还是不满足啊,项目中我们频繁的需要 ...

  7. Linux系统初始化过程及运行级别简介

    Linux开机过程: 1 开机自检(BIOS):初始化硬件,查找启动介质 2 MBR引导 3 GRUB引导菜单:GRUB程序安装在Bootloader 4 加载内核(kernel) 5 运行INIT进 ...

  8. 聊聊VUE中的nextTick

    在谈nextTick之前,先要说明一件事,可能在我们平时使用vue时并没有关注到,事实上,vue执行的DOM更新是异步的. 举个栗子: <template> <div class=& ...

  9. Linux 进程与信号的概念和操作

    进程 主要参考: http://www.bogotobogo.com/Linux/linux_process_and_signals.php 信号与进程几乎控制了操作系统的每个任务. 在shell中输 ...

  10. win10 UWP 发邮件

    UWP 下如何发邮件?可以使用mailto:xx?subject=*方式发送? 本文:如何在 UWP 使用默认邮件发邮件. 打开设置,应用,默认应用,选择应用 OutLook.这样就和我的一样,如果出 ...