HDU 4577 X-boxes (High Precision)

Source: Internet
Author: User

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 --;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.