Sixth case of over 2014 schools | HDU 4927 Series 1 (Yang Hui triangle combination number)

Source: Internet
Author: User

Question Link

The number of N is changed to n-1 at each operation, and finally to a number. The operation refers to the number written by the last number minus the number obtained by the previous number.

Train of Thought: Find a few numbers. Do not calculate them first, instead of using formulas, for example:

1 2 3 4 5 6

(2-1) (3-2) (4-3) (5-4) (6-5)

(3-2-2 + 1) (4-3-3 + 2) (5-4-4 + 3) (6-5-5 + 4)

(4-3-3 + 2-3 + 2 + 2-1) (5-4-4 + 3-4 + 3 + 3-2) (6-5-5 + 4-5 + 4 + 4-3)

(5-4-4 + 3-4 + 3 + 3-2-4 + 3 + 3-2 + 3-2-2 + 1) (6-5-5 + 4-5 + 4 + 4-3-5 + 4 + 4-3 + 4-3-3 + 2)

(6-5-5 + 4-5 + 4 + 4-3-5 + 4 + 4-3 + 4-3 + 2-5 + 4 + 4-3) + 4-3-3 + 2 + 4-3-3 + 2-3 + 2 + 2-1)

Offset the positive and negative values in the number to get the final formula:

1*6-5*5 + 10*4-10*3 + 5*2-1*1

In fact, the coefficient of each number is the Yang Hui triangle. if you write a few more, you can see it. Or you can infer that the above formula is like the Yang Hui triangle.

Then, obtain the number of combinations. Do not create tables or call functions in advance, but directly remove the values in the loop. Otherwise, the timeout will exceed the limit ....

 1 import java.io.*; 2 import java.math.*; 3 import java.text.*; 4 import java.util.*; 5  6 public class Main { 7  8     public static void main(String[] args) { 9         Scanner cin = new Scanner(System.in) ;10         BigInteger[] ch = new BigInteger[3100] ;11         BigInteger ans,a,b;12         int T ,n;13         T = cin.nextInt() ;14         while(T-- > 0)15         {16             n = cin.nextInt();17             ans = BigInteger.ZERO ;18             b = BigInteger.ONE ;19             a = BigInteger.valueOf(n-1) ;20             for(int i = 1 ; i <= n ; i++)21                 ch[i] = cin.nextBigInteger();22             for(int i = 0 ; i < n ; i++)23             {24                 if(i % 2 == 0)25                 {26                     ans = ans.add(b.multiply(ch[n-i])) ;27                 }28                 else 29                 {30                     ans = ans.subtract(b.multiply(ch[n-i])) ;31                 }32                 b = b.multiply(a).divide(BigInteger.valueOf(i+1)) ;33                 a = a.subtract(BigInteger.ONE) ;34             }35             System.out.println(ans);36         }37     }38 39 }
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.