X-Boxes
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 410 Accepted Submission(s): 139
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.
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 )
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
题目的意思很简单,意思我就不说了,只说一下自己的想法。这个题目首先想到的就是把奇数全部都放在第一个箱子里面,当然最后一个箱子不能超过n。然后还可以继续循环到第一个箱子。具体例子我就不说了,可以写一下,n为100,k为3的例子。第一行可以为 1 2 4 8 16 64.很容易发现,8刚好是1的2^3倍数,同样的道理,最后一个数不超过n即可。
题目地址X-Boxes:
写一下c++的伪代码:
len=2^(k-1);
ans=0;
while(n)
{
n=n/len; //一列的
ans+=n-n/2; //计算一列所有的奇数
n/=2; //将列向右平移一次
}
详见JAVA AC代码:
//package anotherday;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int tes,k;
tes=cin.nextInt();
BigInteger ans,n,len1,len2,two,tmp;
two=BigInteger.valueOf(2);
//len1为循环节长度 ,len2为偏移量
while(tes--!=0)
{
n=cin.nextBigInteger(); k=cin.nextInt();
ans=BigInteger.ZERO;
len1=BigInteger.valueOf(1<<(k-1));
len2=len1.multiply(two);
while(!n.equals(BigInteger.ZERO))
{
tmp=n.divide(len1);
ans=ans.add(tmp.subtract(tmp.divide(two)));
n=n.divide(len2);
}
System.out.println(ans);
}
}
}
//1921MS