HDU4577X-boxes Java大数类做法

X-Boxes

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 246    Accepted Submission(s): 84


Problem Description
Crazygirl is a crazy fan of XBOX games. Today, she’s here middle in a competition, in which the winner will be rewarded with an opportunity of working in the XBOX Company as a game testing player. Now, here comes the final game. As Cazygirl get a draw with the other competitor, Lich King, she must beat Lich this time.
The game is quite simple. There are n balls numbered from 1 to n and k boxes B 1, B 2,…, B k satisfying following conditions:
1.  With any ball x in box B i, there must be ball 2x in box B i+1 if there is a box B i+1;
2.  With any ball x in box B i, there must be ball y in box B i-1 satisfying 2y=x if there is a box B i-1;
3.  You can’t put a ball in two different boxes at the same time;
4.  Your score is the number of balls in box B 1;
5.  The player who get the highest score win the game of course.
So, you should tell Crazygirl the highest score she can get.

 

Input
The first line is the number of test cases.
Each test case has one line containing two integers n and k, meaning that there are n balls and k boxes. ( 1≤n≤10 10000, 2≤k≤25 )
 

Output
For each test case, output one line that contains an integer equals to the highest score you can get.
 

Sample Input
  
  
3 10 2 7 5 8 3
 

Sample Output
  
  
4 0 1
 

Source
2013ACM-ICPC杭州赛区全国邀请赛



题目大意:
给你n个球(n非常非常大)和k个箱子,让你尽可能多的往一号箱子里放更多的球,但是必须满足一定的条件,最后问最多能在一号箱子里面放多少球。

想法:
先找规律,发现最优解的情况就是取出比m小的奇数乘以2一个一个放
n=9,k=2  一乘以2的次幂为1,2,4,8,可以放入两个箱子中两次,而3的是3,6,放入一次,列数起来是3次
1 2
4 8
3 6
其实就是找比这些个数去除以k,再相加。 同时为了缩减时间复杂度,我们发现对于 n/(2^k)到 n/2^(k+1)-1之间的数的奇数乘以2的幂数比n小的个数都是相同的,利用这个规律可以简化时间复杂度。

但是n特别大,要用到高精度,Java的BigInterger类是一个封装的高精度计算类,很方便的。

import java.math.*;  
import java.util.Scanner;  
import java.io.*; 

public class Main {
	 public static void main(String[] args)
     {  
        int T;  
        Scanner cin = new Scanner(System.in); 
        T=cin.nextInt();  
        while(T>0)
        { 
        T--;
        int k;
        BigInteger n,one, two;
        two=new BigInteger("2");
        n=cin.nextBigInteger();  
        one=new BigInteger("1");
        k=cin.nextInt();  
        int p=(int)Math.pow(2, k-1),q=p*2;
        BigInteger temp=n.divide(new BigInteger(Integer.toString(p)));
        BigInteger sum=new BigInteger("0");
        temp=temp.add(one);
        sum=temp.divide(two);
        temp=temp.subtract(one);
        while(temp.compareTo(new BigInteger("0"))==1)
        {   
        	
        	temp=temp.divide(new BigInteger(Integer.toString(q)));
        	temp=temp.add(one);
        	sum=sum.add(temp.divide(two));
        	temp=temp.subtract(one);
        }

        System.out.println(sum);
        } 
     }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值