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 ...
随机推荐
- 容器ConcurrentHashMap原理(学习)
一.概述 HashMap 是非线程安全的,在不考虑性能问题的时候,我们的解决方案有 Hashtable 或者Collections.synchronizedMap(hashMap),这两种方式基本都是 ...
- HttpClient-02连接管理
2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...
- 关于Doctype、严格模式与混杂模式
<!Doctype> 文档声明,位于文档中的最前面的位置,处于<html>标签之前.此标签告知浏览器文档使用哪种HTML或XHTML规范. 用于告知浏览器以何种模式来渲染文档. ...
- Android6.0源码分析之录音功能(一)【转】
本文转载自:http://blog.csdn.net/zrf1335348191/article/details/54949549 从现在开始一周时间研究录音,下周出来一个完整的博客,监督,激励!!! ...
- xargs 主要用于不支持管道的shell命令*****
变量置换,主要用于不支持管道的shell命令,如:rm.sed等,但有些命令需要占位符“{}”需要注意.比如:删除文件- ls|xargs -i rm -rf {} 文件改名- ls|xargs ...
- nginx开发(四)调用ffmpeg,搭建rtmp直播流。
1: 修改conf文件,配置rtmp直播 打开usr/local/nginx/conf/nginx.conf,添加红色内容: rtmp {#rtmp点播配置 server { li ...
- J201700526-hm
プレーンテキスト 纯文本 きも 肝 リレーショナル 亲属的,相关的,有关的; ギャップ 缺口; 间隔; 分歧;
- bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】
不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...
- bzoj4720: [Noip2016]换教室(期望dp)
4720: [Noip2016]换教室 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1294 Solved: 698[Submit][Status ...
- [Swift]经典解题思路:联合查找Union Find
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...