2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
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(大数+贪心)的更多相关文章
- 2017CCPC秦皇岛 G题Numbers&&ZOJ3987【大数】
题意: 给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小. 思路: 从二进制的角度分析,如果这m个数中有一个数某一位为1,那么最后或起来这一位肯定是为1的, ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ZOJ 3981 && 2017CCPC秦皇岛 A:Balloon Robot(思维题)
A - Balloon Robot Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Sub ...
- ZOJ - 3987 - Numbers (大数 + 贪心)
参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...
- G - 密码 kmp、贪心、manachar等等等等
G - 密码 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit Status ...
- BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...
- zoj 1025Wooden Sticks(贪心)
递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988
题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...
- 2017CCPC秦皇岛 A题Balloon Robot&&ZOJ3981【模拟】
题意: 一个机器人在长为M的圆形轨道上送气球,当机器人到达M号点的时候下一站会回到1号点,且全程不会停止运动.现在在长为M的轨道上有N个队伍,队伍会在某个时间做需要一个气球,机器人需要送过去.一共有P ...
随机推荐
- oracle pl/sql 存储过程
存储过程用于执行特定的操作,当建立存储过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数,可以将执行部分的数据传递到 ...
- SSH端口转发详解及实例
一.SSH端口转发简介 SSH会自动加密和解密所有SSH客户端与服务端之间的网络数据.但是,SSH还能够将其他TCP端口的网络数据通SSH链接来转发,并且自动提供了相应的加密及解密服务.这一过程也被叫 ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- hdu4678 Mine 2013 Multi-University Training Contest 8 博弈题
Mine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submi ...
- Select的option事件问题
一开始看你们会觉得没问题,我也就是觉得没问题所以才找不到错误所在. 问题出在option本身是没有事件的说法的,只能在select里添加事件,再获取option的属性值 这是我的写法 select设置 ...
- JavaWeb(三)JSP概述
一.JSP概述 1.1.JSP简介 一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束.JSP是一种Java servlet,主要用于实现 ...
- 使用Dapper操作Mysql数据库
首先我想说明一下:相比最原始的ADO.NET,一般都认为封装过一层的ORM性能上会有损耗,但其实在使用中你会发现,当你需要把数据库对象转化为实体模型时,很多所谓的DbHelper其实封装的很低效,反而 ...
- 在前端页面对easyui中的datagrid与jqgrid加载后的数据进行操作
因为项目的需求,需要在grid中加载数据后再在前端页面执行操作,所以在easyui中的grid与jqgrid都进行了测试和操作: eayui中grid数据的操作: //构造集合对象 var list ...
- Ghost文件封装说明
一.先列举目前Windows系统安装方式: 1.光盘安装 1.1 使用可刻录光驱将系统ISO文件刻录至DVD光盘,刻录工具比较多,QA目前使用Ultra ISO. 1.2 安装电脑从DVD光盘启动,无 ...
- 百度地图JS调用示例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...