2016 CCPC 秦皇岛 G Numbers 【贪心+大数+高精度】
来源:互联网 发布:淘宝买的lol号安全吗 编辑:程序博客网 时间:2024/05/17 01:48
题目链接:ZOJ 3987 Numbers
Problem Description
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. n = a1 + a2 + … + am, and a1 OR a2 OR … OR am should be as small as possible).
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m(0 ≤ n < 10^1000,1 ≤ m < 10^100).
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
题目大意:
给你一个数n,将它分成m个非负数的和,要求这些数的逻辑或后的值最小,输出这个最小的值。
解题思路:
Mycode
import java.util.*;import java.math.*;public class Main{ static BigInteger bit[] = new BigInteger [4005]; static BigInteger one = BigInteger.ONE; static BigInteger two = BigInteger.valueOf(2); static BigInteger zero = BigInteger.ZERO; static BigInteger n, m; public static void Init() { bit[0] = one; for(int i = 1; i <= 4000; ++i) { bit[i] = bit[i-1].multiply(two); } } public static void Work() { BigInteger sum = zero, tmp = n, ans = zero; int up = 0; for(int i = 0; sum.compareTo(n) < 0; ++i) { sum = sum.add(m.multiply(bit[i])); up = i; } for(int i = up; i >= 0; --i) { BigInteger t = bit[i].subtract(one); if(t.multiply(m).compareTo(tmp) >= 0) continue; BigInteger k = tmp.divide(bit[i]); k = k.min(m); tmp = tmp.subtract(bit[i].multiply(k)); ans = ans.add(bit[i]); } System.out.println(ans); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); Init(); int t = sc.nextInt(); for(int i = 1; i <= t; ++i) { n = sc.nextBigInteger(); m = sc.nextBigInteger(); Work(); } }}
阅读全文
0 0
- 2016 CCPC 秦皇岛 G Numbers 【贪心+大数+高精度】
- ZOJ 3987 && 2017CCPC秦皇岛 G:Numbers(高精度+贪心)
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制乱搞、贪心)
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制、贪心)
- ZOJ 3987 Numbers 2017CCPC秦皇岛站G题 大整数 二进制 贪心
- 2017 CCPC 秦皇岛 G题
- 2017 CCPC 秦皇岛 G题
- Persistent Numbers(贪心+高精度)
- CCPC 秦皇岛站体验
- 2017 CCPC 秦皇岛站
- 2017 CCPC秦皇岛 B
- 2017 CCPC秦皇岛 D
- 2017CCPC秦皇岛
- 2017 CCPC 秦皇岛
- 2017 CCPC 秦皇岛 C
- 2017 CCPC 秦皇岛 E
- 2017 CCPC 秦皇岛 L
- Nginx+Tomcat+memcached负载均衡实现session共享
- 基于Unity3D的相机震动效果的实现
- 机器学习相关概念梳理
- 设计模式-代理模式
- JAVA知识点概述
- 2016 CCPC 秦皇岛 G Numbers 【贪心+大数+高精度】
- 菜鸟学习历程【4】数组(2)、函数
- matplotlib+pyqt4 内容整理
- 201711122220->mysql删除database
- 程序员如何转型人工智能(机器学习)
- 双向链表(C语言实现)学习记录
- 设计模式-适配器模式
- ExtJs之下拉框ComboBox
- markdown语法之数学符号汇总