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 ...
随机推荐
- getOutputStream() has already been called for this response
错误日志里偶尔会有getOutputStream() has already been called for this response这个错误 最近发现了高概率复现条件,所以顺手解决了一下: 首先根 ...
- 点击截图功能 js canvas
使用:html2canvas实现浏览器截图 <html> <head> <meta name="layout" content="main& ...
- 高德地图JSApi
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Qt学习之路MainWindow学习过程中的知识点
一.Qt的GUI程序有一个常用的顶层窗口,叫做MainWindow MainWindow继承自QMainWindow.QMainWindow窗口分成几个主要的区域: 二.QAction类 QAct ...
- Muddy Fields
Muddy Fields Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU1166 数状数组
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- ZOJ2334 Monkey King 并查集 STL
题意:两家原始人(猴)打交道后成为一家猴,打交道时两家分别派出最帅的两位猴子,颜值各自减半,问每次打交道后新家族最帅的猴子的颜值.当然,已经是一家子就没有必要打交道了,因为没有猴希望颜值降低,毕竟还得 ...
- sqlDependency监控数据库数据变化,自动通知
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- BitmapImage 读取内存流和显示图片
FileStream filestream = File.OpenRead(@"C:\Users\Administrator\Desktop\queryHeaderImg.png" ...
- python装饰器 & flask 通过装饰器 实现 单点登录验证
首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...