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 ...
随机推荐
- bzoj 1017 魔兽地图DotR
题目大意: 他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄的力量值等于它购买的所有装备的力量值之和.装备分为基本装备和高级装备两种.基本装备可以直接从 ...
- Linux 下編輯 PDF 檔的工具(PDF editor under Linux)(转载)
转自:http://www.gtwang.org/2011/05/linux-pdf.html PDF 檔雖然是一個跨平台的檔案格式,但 Adobe 只有提供免費的 Adobe Reader,要看 P ...
- bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】
--这可能是早年Pascal盛行的时候考排序的吧居然还是Glod-- #include<iostream> #include<cstdio> #include<algor ...
- P1128 [HNOI2001]求正整数
传送门 rqy是我们的红太阳没有它我们就会死 可以考虑dp,设\(dp[i][j]\)表示只包含前\(j\)个质数的数中,因子个数为\(i\)的数的最小值是多少,那么有转移方程 \[f[i][j]=m ...
- [Swift通天遁地]一、超级工具-(10)使用地图视图MKMapView的相机功能实现创建三维地图
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Font Awesome矢量图标
下载 font-awesome 文件夹到您的项目中. 在HTML的 <head> 中引用font-awesome.min.css. 可以将Font Awesome图标使用在几乎任何地方,只 ...
- Parameterized testing with any Python test framework
1. 在进行单元测试时,很多时候需要进行参数化 尝试过使用 from nose_parameterized import parameterized 但在使用过程中会报错,后来将上面的内容改为了下面的 ...
- fprintf写入字符串入文件/fread读取文件内的字符串
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { FILE * ...
- Linux上安装wine qq的方法
linxu上安装QQ的发 百度网盘 提取码:f2sn 步骤一.安装wine(详见:https://www.winehq.org/download) // ubuntu/ubuntukylin/mint ...
- magento 翻译使用实例
在自定义的模块中若想要使用翻译,需在config.xml中加入如下配置 <config> <adminhtml> //后台 <translate> <modu ...