码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4927 Series 1

时间:2014-08-08 09:39:45      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   java   使用   os   io   strong   

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来计算就比较合适了。
*/



hdu 4927 Series 1,布布扣,bubuko.com

hdu 4927 Series 1

标签:des   style   color   java   使用   os   io   strong   

原文地址:http://blog.csdn.net/knight_kaka/article/details/38434337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!