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 ...
随机推荐
- 洛谷P1514 引水入城——dfs
题目:https://www.luogu.org/problemnew/show/P1514 搜索+DP: 自己想出来的方法第一次80分好高兴! 再改了改就A了,狂喜乱舞: 也就是 dfs,仔细一想第 ...
- bzoj4619
4619: [Wf2016]Swap Space Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 129 Solved: 54[Submit][Sta ...
- Same origin policy
Chrome: Origin null is not allowed by Access-Control-Allow-Origin 的问题 发送 http request 时遇到 error: Ori ...
- KeepAlived的介绍
KeepAlived介绍 keepalived keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层.第4层和第7层交换. Keepalived的 ...
- 如何过滤 adb logcat 输出(转载)
转自:http://www.cnblogs.com/imouto/archive/2012/12/11/filtering-adb-logcat-output.html 简介: 本文介绍如何在 she ...
- bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路【dijskstra】
严格次短路模板,用两个数组分别维护最短路和次短路,用dijskstra,每次更新的时候先更新最短路再更新次短路 写了spfa版的不知道为啥不对-- #include<iostream> # ...
- Akka源码分析-Cluster-DistributedData
上一篇博客我们研究了集群的分片源码,虽然akka的集群分片的初衷是用来解决actor分布的,但如果我们稍加改造就可以很轻松的开发出一个简单的分布式缓存系统,怎么做?哈哈很简单啊,实体actor的id就 ...
- python自动化测试学习笔记-9测试框架
学习了这么久的python,我们已经可以自己搭建一个简单的测试和框架了,先从简单的开始,有时我们编写接口的测试用例会用excel进行编写,以下面的接口测试用例模板为例,进行编写:
- 【NOIP模拟赛】一道挖掉背景的数学题
Title:[Empty] Time Limit:1000 ms Memory Limit:131072 KBytes Description 给定n与p,求\(\left\lfloor x^n\ri ...
- U - Relatives(欧拉函数)
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...