HDU 4927 Series 1

Series 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 118    Accepted Submission(s): 33


Problem Description
Let A be an integral series {A1, A2, . . . , An}.

The zero-order series of A is A itself.

The first-order series of A is {B1, B2, . . . , Bn-1},where Bi = Ai+1 - Ai.

The ith-order series of A is the first-order series of its (i - 1)th-order series (2<=i<=n - 1).

Obviously, the (n - 1)th-order series of A is a single integer. Given A, figure out that integer.
 

Input
The input consists of several test cases. The first line of input gives the number of test cases T (T<=10).

For each test case:
The first line contains a single integer n(1<=n<=3000), which denotes the length of series A.
The second line consists of n integers, describing A1, A2, . . . , An. (0<=Ai<=105)
 

Output
For each test case, output the required integer in a line.
 

Sample Input
   
   
   
   
2 3 1 2 3 4 1 5 7 2
 

Sample Output
   
   
   
   
0 -5
 

Source
2014 Multi-University Training Contest 6


题目链接  :http://acm.hdu.edu.cn/showproblem.php?pid=4927


题目大意  :输入一串数,用后一个数减前一个数组成一个新的序列,一直这样执行下去直到序列中只剩一个数,输出那个数


题目分析  :比赛开始时,这题wa了一片,后来发现这题是一道大数题,其次暴力做肯定超时,通过对系数的考察,我们发现系数的绝对值是杨辉三角形,杨辉三角形的每一行是可以用组合数算出来,这里每次递归算组合数也会超时,所以我们一次算完保留原来的值然后每次算时直接更新,这样就不会超时了,用到一个组合数公式,系数的标号为m(1 <= m <= len - 1),序列长度为n则第m个数的系数为C(m-1,n-1)
C(m-1,n-1) = (n-1)! / (m-1)! * (n-m)!      又C(m,n-1) = (n-1)! / (m)! * (n-m-1)!  所以C(m,n-1) = C(m-1.n-1) * (n-m) / m,推出这个公式这题就基本解决了,还有一点就是符号问题,要注意序列奇偶的不同,下面贴代码


import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        BigInteger[] num = new BigInteger[3005];
        BigInteger ans;
        int T = in.nextInt();
        for(int i = 0; i < T; i++ )
        {
            ans = BigInteger.ZERO;
            int flag;
            int len = in.nextInt();
            if(len % 2 == 0)  //若序列长度为偶数num[1]为负,奇数为正
                flag = -1;
            else
                flag = 1;
            for(int j = 1; j <= len; j++)
                num[j] = in.nextBigInteger();
            BigInteger temp = BigInteger.ONE;
            for(int m = 1; m <= len; m++)
            {
                ans = ans.add(num[m].multiply(temp).multiply(BigInteger.valueOf(flag)));
                flag *= -1;
                temp = temp.multiply(BigInteger.valueOf(len-m)).divide(BigInteger.valueOf(m));
            }
            System.out.println(ans);
        }
    }
}


你可能感兴趣的:(组合数,hduoj)