2017 CCPC 秦皇岛 & ZOJ 3987 - Numbers (贪心+大数)
2017-12-19 23:26
447 查看
Numbers
Time Limit: 2 Seconds Memory Limit: 65536 KB
DreamGrid has a nonnegative integer .
He would like to divide into nonnegative
integers and
minimizes their bitwise or (i.e. and should
be as small as possible).
indicating the number of test cases. For each test case:
The first line contains two integers and ().
It is guaranteed that the sum of the length of does
not exceed .
Author: LIN, Xi
Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site
POINT:
第一次用java写大数,好方便啊!
这题简单的贪心就行了。
从高位开始判断,判断这一位的0可以不可以给:
可以给的状态是 假设这一位后面的数全是1111,这1111*m不小于当前要填满的n,那么这个0就可以放。
Time Limit: 2 Seconds Memory Limit: 65536 KB
DreamGrid has a nonnegative integer .
He would like to divide into nonnegative
integers and
minimizes their bitwise or (i.e. and should
be as small as possible).
Input
There are multiple test cases. The first line of input contains an integer ,indicating the number of test cases. For each test case:
The first line contains two integers and ().
It is guaranteed that the sum of the length of does
not exceed .
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
Author: LIN, Xi
Source: The 2017 China Collegiate Programming Contest, Qinhuangdao Site
POINT:
第一次用java写大数,好方便啊!
这题简单的贪心就行了。
从高位开始判断,判断这一位的0可以不可以给:
可以给的状态是 假设这一位后面的数全是1111,这1111*m不小于当前要填满的n,那么这个0就可以放。
import java.math.BigInteger; import java.util.Scanner; import java.math.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int T; while(cin.hasNext()){ T=cin.nextInt(); while(T!=0) { BigInteger n = cin.nextBigInteger(); BigInteger m = cin.nextBigInteger(); BigInteger now = BigInteger.valueOf (1); BigInteger er =BigInteger.valueOf(2); BigInteger sum = m.multiply(now); int k=1; while(sum.compareTo(n)==-1) { k++; now=now.multiply(er); sum=sum.add(now.multiply(m)); } BigInteger ans = BigInteger.ZERO; // System.out.println(now); while(n.compareTo(BigInteger.ZERO)!=0) { BigInteger num = n.divide(now); if(num.compareTo(m)==1) { num=m; } BigInteger now1 = now.subtract(BigInteger.ONE); if(now1.multiply(m).compareTo(n)!=-1) { now=now.divide(er); continue; } ans=ans.add(now); n=n.subtract(num.multiply(now)); now=now.divide(er); } System.out.println(ans); T--; } } } }
相关文章推荐
- 2017 CCPC 秦皇岛 G Numbers 【贪心+大数+高精度】
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
- ZOJ 3987 && 2017CCPC秦皇岛 G:Numbers(高精度+贪心)
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
- ZOJ 3987 Numbers 2017CCPC秦皇岛站G题 大整数 二进制 贪心
- 2017 CCPC 秦皇岛 & ZOJ 3981 - Balloon Robot 规律
- 2017 CCPC 秦皇岛 & ZOJ 3983 - Crusaders Quest
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制乱搞、贪心)
- 2017 CCPC 秦皇岛 & ZOJ 3985 - String of CCPC (子串)
- 2017 CCPC 秦皇岛 & ZOJ 3993 - Safest Buildings (概率+规律)
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制、贪心)
- ZOJ 3987 Numbers (大数+贪心)
- ZOJ 3985 String of CCPC 2017秦皇岛CCPC(子串个数)
- 2017CCPC秦皇岛 G题Numbers&&ZOJ3987【大数】
- hdu6237-2017哈尔滨CCPC-质因子&贪心&暴力-A Simple Stone Game
- ZOJ 3987 Numbers (贪心)
- zoj&CCPC秦皇岛站E-思维-String of CCPC
- 2017 CCPC 秦皇岛站 M题题解 ZOJ 3993 Safest Buildings
- 2017 CCPC 秦皇岛 M - Safest Buildings 【几何&概率】
- ZOJ - 3987 - Numbers (高精度,贪心)