首页 > 代码库 > hdu 4927 Series 1

hdu 4927 Series 1

Series 1

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


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
 

Author
BUPT
 


题解及代码:


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

public class Main {

    
    public static BigInteger solve(BigInteger s[],int n)
    {
        BigInteger ans=BigInteger.valueOf(0);
        BigInteger t=BigInteger.valueOf(1);
        BigInteger l=BigInteger.valueOf(1);
        BigInteger r=BigInteger.valueOf(n-1);
        int k=0;
        for(int i=n-1;i>=0;i--)
        {
           if(k%2==0) ans=ans.add(t.multiply(s[i]));
           else ans=ans.subtract(t.multiply(s[i]));
           t=t.multiply(r).divide(l);
           l=l.add(BigInteger.ONE);
           r=r.subtract(BigInteger.ONE);
           k++;
        }
        return ans;
    }
    
    public static void main(String []args)
    {
        int T,n;
        BigInteger s[]=new  BigInteger[3010];
        Scanner cin = new Scanner(System.in); 
        T=cin.nextInt();
        while(T>0)
        {
            T--;
            n=cin.nextInt();
            for(int i=0;i<n;i++)
            {
                s[i]=cin.nextBigInteger();
            }
            System.out.println(solve(s,n));
        }
    }
}
/*
简单计算一下:
以1 2 3 为例子
第二层为 (2-1) (3-2)
第三层为     3-2*2+1
还不是很明显,再以1 5 7 2 为例子:
第二层为 (5-1) (7-5) (2-7)
第三层为    (7-2*5+1) (2-2*7+5)
第四层为       2-3*7+3*5-1
这样我们就找到了规律,那就是计算从记过从右向左排列,
,每个数前面的系数依次为组合数,符号很奇偶。

由于计算结果比较大,使用java来计算就比较合适了。
*/