Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2446

Source: Internet
Author: User

/* <Br/> * A Very watery question, why is it true that I am using AC? <br/> * Self-blaming ing <br/> * 1 + 1 + 2 + 1 + 2 + 3 + .... = n (n + 1) (N + 2)/6; <br/> * finding the smallest N in the second vertex satisfies n (n + 1) (N + 2) /6> = YY <br/> * search for R (R + 1) in binary mode) /2 <= XX <br/> * benefits when the maximum r <br/> * is long multiplied, I always thought that my code was wrong. <br/> * zhc is right. Why should I use long when I use biginteger? <br/> * exercise java well, neither of them knows <br/> */<br/> Import Java. util. *; <br/> Import Java. io. *; <br/> Import Java. math. *; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> using CIN = new using (system. in); <br/> int t; <br/> T = cin. nextint (); <br/> while (t --> 0) {<br/> // biginteger N; <br/> // n = cin. nextbiginteger (); <br/> long n; <br/> N = cin. nextlong (); <br/> long lay = Bin (n); <br/> system. out. print (lay + ""); <br/> biginteger a1 = biginteger. valueof (lay-1); <br/> biginteger a2 = biginteger. valueof (lay); <br/> biginteger a3 = biginteger. valueof (lay + 1); <br/> biginteger two = biginteger. valueof (2); <br/> biginteger six = biginteger. valueof (6); <br/> biginteger ans = a1.multiply (A2 ). multiply (A3 ). divide (six); </P> <p> biginteger nn = biginteger. valueof (n); <br/> nn = nn. subtract (ANS); <br/> long R = bin2 (NN); <br/> // If (R * (R + 1)/2 = nn. longvalue () <br/> // R-= 1; <br/> biginteger RR = biginteger. valueof (r); <br/> If (RR. multiply (RR. add (biginteger. one )). divide (two ). compareto (NN) = 0) <br/> r-= 1; <br/> ans = nn. subtract (biginteger. valueof (R ). multiply (biginteger. valueof (R + 1 )). divide (two); <br/> r + = 1; <br/> system. out. println (R + "" + ans); <br/>}</P> <p> static long bin2 (biginteger N) {<br/> biginteger num = N; <br/> biginteger two = biginteger. valueof (2); <br/> long low = 1; <br/> long high = n. longvalue (); <br/> long ans = 0; <br/> while (low <= high) {<br/> long mid = (low + high)/2; <br/> biginteger a1 = biginteger. valueof (MID); <br/> biginteger a2 = biginteger. valueof (Mid + 1); <br/> biginteger sum = a1.multiply (A2); <br/> If (sum. compareto (Num. multiply (two) = 0) {<br/> return mid; <br/>} else if (sum. compareto (Num. multiply (two) =-1) {<br/> ans = mid; <br/> low = Mid + 1; <br/>}else {<br/> high = mid-1; <br/>}< br/> return ans; <br/>}< br/> static Long Bin (long n) {<br/> biginteger num = biginteger. valueof (n); <br/> biginteger six = biginteger. valueof (6); <br/> long low = 1; <br/> long high = (long) math. SQRT (1.0 * n) + 1; <br/> long ans = 0; <br/> while (low <= high) {<br/> long mid = (low + high)/2; <br/> biginteger a1 = biginteger. valueof (MID); <br/> biginteger a2 = biginteger. valueof (Mid + 1); <br/> biginteger a3 = biginteger. valueof (Mid + 2); <br/> biginteger sum = a1.multiply (A2 ). multiply (A3); <br/> If (sum. compareto (Num. multiply (six) = 0) {<br/> // ans = mid; <br/> // high = mid-1; <br/> return mid; <br/>} else if (sum. compareto (Num. multiply (six) = 1) {<br/> ans = mid; <br/> high = mid-1; <br/>}else {<br/> low = Mid + 1; <br/>}< br/> return ans; <br/>}< br/> 

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.