Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4577
N balls, K boxes, and balls meet the following rules:
1. Put the ball numbered X in the I box, so the I + 1 box must be numbered 2x.
Ask how many balls you can put in the first box at most.
Question: place the number 1 in the first box, then fill K boxes according to the above rules, and then put the smallest number of balls in the first box ......, Until K boxes cannot be filled.
The first can be placed at most (N/(2 ^ (k-1) + 1)/2 (the odd number can certainly be put), and then look at the excess, can be placed 1, 3, 5, 7 multiplied by their 2 ^ (jk) (j = 1, 2, 3, 4 ,...), when a loop is run, two parts are added, that is, the answer.
ACCode:
Import Java. math. biginteger; import Java. util. internal; public class main {static partition CIN = new partition (system. in); public static void main (string [] ARGs) {int T, K; biginteger sum, X, N, P, two = biginteger. valueof (2), zero = biginteger. zero; t = cin. nextint (); While (T! = 0) {n = cin. nextbiginteger (); k = cin. nextint (); sum = zero; X = biginteger. valueof (1 <(k-1); While (true) {n = n. divide (x); P = n. subtract (N. divide (two); If (P. equals (zero) break; sum = sum. add (p); n = n. divide (two);} system. out. println (SUM); t --;}}}