Hdu4927 (Series 1)

Source: Internet
Author: User

Question address: Series 1

 

Question:

There is a set of sequence A: {A1, a2...... }. 0th sequences for itself, the first sequence is the last one minus the previous one (e.g.: A2-A1, A3-A2... an-1) and so on, the n-1 sequence must be a number. Output this number.

 

Solution:

The number ans = aa1 + A2A2... Anan calculated. Then, the preceding coefficient is the coefficient of the nth row in the Yang Hui triangle (binary coefficient). the symbol of the last coefficient is positive, one is positive, one is negative, and so on.

Use Java to handle large numbers.

 

Code:

1 import Java. util. *; 2 Import Java. math. *; 3 4 public class main 5 {6 public static void main (string [] ARGs) 7 {8 int N, I, j, T; 9. Cin = new partition (system. in); // declare the input. 10 biginteger P [] = new biginteger [3005]; // declare a large number array 11 T = cin. nextint (); // enter T12 while (T! = 0) 13 {14 t --; 15 N = cin. nextint (); // enter n16 biginteger Minn = biginteger. valueof (0), Maxx = biginteger. valueof (0), c = biginteger. valueof (1), TMP, ans; // Add the initial value 17 18 for (I = 1; I <= N; I ++) 19 P [I] = cin. nextbiginteger (); 20 21 for (I = 0; I <= n-1; I ++) // start from 0. to calculate the coefficient symbol of the Yang Hui triangle, start from the last positive. 22 {23 if (I> 0) // The divisor cannot be 024 {25 biginteger xxx = biginteger. valueof (n-I); // The maximum number of upper denominator is 26 C = C. multiply (XXX)/* multiply */. divide/* Division */(biginteger. valueof (I); // remove the smallest number of molecules. // calculate the last coefficient and calculate the coefficient 27} 28 TMP = C. multiply (P [n-I]); 29 if (I % 2 = 1) 30 Minn = Minn. add (TMP); // Add 31 else32 Maxx = Maxx. add (TMP); 33} 34 Maxx = Maxx. subtract (Minn); // The positive value minus the negative value 35 system. out. println (Maxx. tostring (); // output 36} 37} 38}
View code

 

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.