HDU 4577 X-Boxes 解题报告(数学)
2013-08-14 10:54
225 查看
题目:
Total Submission(s): 305 Accepted Submission(s): 106
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 B1, B2,…, Bk satisfying following conditions:
1. With any ball x in box Bi, there must be ball 2x in box Bi+1 if there is a box Bi+1;
2. With any ball x in box Bi, there must be ball y in box Bi-1 satisfying 2y=x if there is a box Bi-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 B1;
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≤1010000, 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
解题报告:题目意思很好懂,不过下手不容易了。
大数据,用C++的话敲模板应该也可以。用Java更方便一点。
首先推一下。[2n/2^(2k),2n/2^k]区间内的奇数都可以放在第一个箱子里,然后[2n/2^(3k),2n/2^(2k)]内的奇数都可以放在第一个箱子里,而且可以放置两轮。比如8 2。1 2 4 8,1和4都可以放在第一个箱子里。
代码如下:
看了大牛的解题报告,代码可以优化下。
X-Boxes
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 305 Accepted Submission(s): 106
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 B1, B2,…, Bk satisfying following conditions:
1. With any ball x in box Bi, there must be ball 2x in box Bi+1 if there is a box Bi+1;
2. With any ball x in box Bi, there must be ball y in box Bi-1 satisfying 2y=x if there is a box Bi-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 B1;
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≤1010000, 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
解题报告:题目意思很好懂,不过下手不容易了。
大数据,用C++的话敲模板应该也可以。用Java更方便一点。
首先推一下。[2n/2^(2k),2n/2^k]区间内的奇数都可以放在第一个箱子里,然后[2n/2^(3k),2n/2^(2k)]内的奇数都可以放在第一个箱子里,而且可以放置两轮。比如8 2。1 2 4 8,1和4都可以放在第一个箱子里。
代码如下:
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger two = new BigInteger("2"); static Scanner cin = new Scanner(System.in); public static void main(String[] args) { int T; T = cin.nextInt(); while(T-->0) { BigInteger big = cin.nextBigInteger(); big=big.multiply(two); int k=cin.nextInt(); BigInteger index=BigInteger.ONE; BigInteger ans=BigInteger.ZERO; BigInteger v=two.pow(k); BigInteger Up; BigInteger Down=big.divide(v); if(Down.equals(BigInteger.ZERO)) { System.out.println(0); continue; } while(true) { Up = Down; Down = Up.divide(v); if(Down.equals(BigInteger.ZERO)) { ans=ans.add(Up.add(BigInteger.ONE).divide(two).multiply(index)); break; } if(!Up.mod(two).equals(BigInteger.ZERO)) Up=Up.add(BigInteger.ONE); ans=ans.add(Up.subtract(Down).divide(two).multiply(index)); index=index.add(BigInteger.ONE); } System.out.println(ans); } } }
看了大牛的解题报告,代码可以优化下。
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger two = new BigInteger("2"); static Scanner cin = new Scanner(System.in); public static void main(String[] args) { int T; T = cin.nextInt(); while(T-->0) { BigInteger n = cin.nextBigInteger(); int k=cin.nextInt(); BigInteger temp = two.pow(k); BigInteger ans = BigInteger.ZERO; n = n.multiply(two); while(n.compareTo(BigInteger.ZERO) > 0) { n = n.divide(temp); BigInteger tt=n.add(BigInteger.ONE).divide(two); ans = ans.add(tt); } System.out.println(ans); } } }
相关文章推荐
- HDU 4577 X-Boxes (数学+大数除法)
- HDU 5781 数学期望+DP 解题报告
- hdu 1068 Girls and Boys 解题报告
- Monkey and Banana(HDU 1069)解题报告(DP - 滚动数组)
- HDU解题报告——1008
- Uva 11752 The Super Powers 解题报告(数学)
- HDU 1394 Minimum Inversion Number解题报告
- HDU 1754 解题报告 线段树 单点更新
- HDU1879 继续畅通工程 解题报告--prim
- HDU1596 find the safest road 解题报告--Dijkstra
- HDU 2602 Bone Collector 解题报告(dp入门题)
- HDU 5009 Paint Pearls 解题报告(DP)
- HDU - 1532 Drainage Ditches解题报告(网络流 最大流魔板题)
- hdu 1015 Safecracker 解题报告
- HDU 1075 Trie树 解题报告
- HDU 2049 不容易系列之(4)考新郎——解题报告
- hdu 1203 解题报告 I NEED A OFFER!
- HDU 3516 四边形优化 解题报告
- HDU-1728 逃离迷宫 解题报告(张浩盛伦)
- HDU-2255 奔小康赚大钱(完备匹配下的最大权匹配) 解题报告