ZOJ - 3987 - Numbers (大数 + 贪心)
参考自:https://blog.csdn.net/u013534123/article/details/78484494
题意:
给出两个数字n,m,把n分成m份,使得以下最小

思路:
或运算只有0|0=0,如果这一位有一个1,那么结果中这一位一定是1,所以我们要尽可能把1集中在几个位上(以二进制看结果)
(二进制)主要的思想是把高位设置成1,这样可以分担大部分数值
用 i-1 位全部为1的二进制数t × m 与n进行判断
如果n大,说明n可以分成 pow(2,i)*m+x,x为不确定数字,这样说明把m个数字第i为设置为1
如果n小,说明分不出来,继续使i变小判断
代码:
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static BigInteger two = BigInteger.valueOf(2);
public static BigInteger p[] = new BigInteger[5000];
public static void init() //预处理二进制下每一位都为1的值
{
p[1] = BigInteger.ONE;
p[0] = BigInteger.ZERO;
for (int i = 2; i < 5000; i++)
p[i] = p[i - 1].multiply(two);
for (int i = 2; i < 5000; i++)
p[i] = p[i].add(p[i - 1]); //p[i]不仅是二进制i位为1,还记录了加和
}
public static void main(String[] args) {
init();
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for (int ca = 1; ca <= T; ca++) {
BigInteger ans = BigInteger.ZERO;
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
int up = 0;
for (int i = 0; i < 5000; i++) //首先找到最高位
if (p[i].compareTo(a) > 0) { //找到第一个比a大的数
up = i;
break;
}
for (int i = up; i >= 1; i--) {
ans = ans.multiply(two); //还原二进制为十进制
if (a.compareTo(p[i - 1].multiply(b)) <= 0) continue; //若后面可以大于n剩余的量,那么这一位放0
BigInteger now = p[i].subtract(p[i - 1]); //否则就只能放1,而且要让n尽量减去更多,剩下更少
BigInteger k = a.divide(now);
ans = ans.add(BigInteger.ONE);
if (k.compareTo(b) > 0) a = a.subtract(now.multiply(b));
else a = a.subtract(now.multiply(k));
}
System.out.println(ans);
}
}
}
ZOJ - 3987 - Numbers (大数 + 贪心)的更多相关文章
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...
- ZOJ 3987 Numbers(Java枚举)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987 题意:给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n ...
- zoj Fibonacci Numbers ( java , 简单 ,大数)
题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; imp ...
- ZOJ 2702 Unrhymable Rhymes 贪心
贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds Memory Limit: 32768 KB Special Judge ...
- PAT 1023 Have Fun with Numbers[大数乘法][一般]
1023 Have Fun with Numbers (20)(20 分) Notice that the number 123456789 is a 9-digit number consistin ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- ZOJ 3689 Digging(贪心+dp)
Digging Time Limit: 2 Seconds Memory Limit: 65536 KB When it comes to the Maya Civilization, we ...
- Have Fun with Numbers (大数)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
随机推荐
- Delphi各销售版本之间的区别
初步的区别: http://www.embarcadero.com/products/delphi/product-editions http://www.embarcadero.com/fr/pro ...
- 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)
环境:sql server 2008 一.创建网络驱动器映射 语法:exec master..xp_cmdshell 'net use Z: \\ip地址\网络路径 密码 /user:用户名' 例如: ...
- noip2002矩阵覆盖(搜索)
矩阵覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见 ...
- P3573 [POI2014]RAJ-Rally
传送门 很妙的思路 首先这是一个DAG,于是我们先在原图和反图上各做一遍,分别求出\(diss_i\)和\(dist_i\)表示从\(i\)点出发的最短路和以\(i\)为终点的最短路 我们考虑把点分为 ...
- oozie timezone时区配置
cloudera oozie默认时区是UTC,在开发oozie任务时必须在期望执行的时间上减去8小时,很不习惯.记录下修改时区的配置操作. 1. cloudera oozie配置—>Oozie ...
- redis存储对象(转)
原文地址:http://www.cnblogs.com/JKayFeng/p/5911544.html 为什么要实现序列化接口 当一个类实现了Serializable接口(该接口仅为标记接口,不包含任 ...
- ROS-URDF仿真
前言:URDF (标准化机器人描述格式),是一种用于描述机器人及其部分结构.关节.自由度等的XML格式文件. 一.首先做一个带有四个轮子的机器人底座. 1.1 新建urdf文件 在chapter4_t ...
- 2017西安网络赛C_SUM
样例输入 1 1 样例输出 89999999999999999999999999 题意:利用上述公式,求出k的值 思路:找规律,找规律发现233个9,无论x是何值永远成立 (这种规律题尽量就不用跟队友 ...
- 锚点、target="page1"、浮标回到顶部(该点未实现,能力不足)
<html> <head> <meta charset="utf-8"> <title>链接</title> <! ...
- java IO流技术 之 File
IO流技术 概念:input - output 输入输出流: 输入:将文件读到内存中: 输出:将文件从内存中写出到其他地方 作用:主要就是解决设备和设备之间的数据传输问题. File :文件类的使用十 ...