hdu4927 Series 1(组合+公式 Java大数高精度运算)

题目链接:

Series 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 423    Accepted Submission(s): 146

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

题目分析:

公式:C(n-1,0)*a[n]-C(n-1,1)*a[n-1]+C(n-1,2)*a[n-2]+...+C(n-1,n-1)*a[n]。

用C(n,k)=C(n,k-1)*(n-k+1)/k即可快速得到一行的二项式系数。

第一道Java题!

代码如下:

import java.util.Scanner;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        Scanner cin =  new Scanner(System.in);
        BigInteger[] a;
        a = new BigInteger[3017];
        int t, n;
         t = cin.nextInt();

         while(t--){
            n = cin.nextInt();

            for (int i = 0; i < n; i++)
                a[i] = cin.nextBigInteger();

            BigInteger ans = BigInteger.ZERO;
            BigInteger c =  BigInteger.ONE; 

            for (int i = 0; i < n; i++) {
                BigInteger tmp = c.multiply(a[n-i-1]);

                if (i%2 == 0)
                    ans = ans.add(tmp);
                else
                    ans = ans.subtract(tmp);

                tmp = c.multiply(BigInteger.valueOf(n-i-1));
                c = tmp.divide(BigInteger.valueOf(i+1));
            }

            System.out.println(ans);
        }
    }
}

hdu4927 Series 1(组合+公式 Java大数高精度运算),布布扣,bubuko.com

时间: 2024-10-07 05:07:55

hdu4927 Series 1(组合+公式 Java大数高精度运算)的相关文章

hdu 4927 Series 1(组合,java大数)

http://acm.hdu.edu.cn/showproblem.php?pid=4927 比赛时分分钟推出来公式后,陷入无限的tle中.起初想处理一遍,但是3000的数量内存装不下,直接编译就不过.为了不超内存,然后又试着把三个数合并为一个,四个数合并一个,100个数合并为一个,还是TLE.. 说到底还是数学不好啊,杨辉三角的第n行的第m个数为组合数c[n-1][m-1]. 应用c[n][m] = c[n][m-1]*(n-m+1)/m,就可以快速递推出第n行的数,这样就能省去预处理的时间以

hdu 4927 Series 1(组合+公式)

题目链接:hdu 4927 Series 1 题目大意:给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1?ai,直到序列长度为1.输出最后的数. 解题思路:n最多才3000,ai最大也才1000,貌似不会超int,但是要注意,有些数不止被计算了一次,最多的数被计算了C(15003000),所以肯定要用高精度处理,那么用o(n2)的复杂度肯定就跪了.其实对于最后的ans,ans=∑i=0n?1C(in?1)?ai?(?1)i+1(类似杨辉三角) import

大数高精度运算(模板)

前言:高精度运算.是指參与运算的数(加数.减数,因子--)范围大大超出了标准数据类型(整型,实型)能表示的范围的运算. 模板:包含大数加减乘除.大数与int数的乘法,模板能够不断扩充. 代码: /* 所有亲測可用,可是不能用于负数的运算,仅仅能对正数进行大数运算 */ const int ten[4]= {1,10,100,1000}; const int maxl = 300; struct BigNumber { int d[maxl]; char s[maxl]; BigNumber(co

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a

java大数

java大数还是很好用的! 基本加入: import java.math.BigInteger; import jave.math.BigDecimal; 分别是大数和大浮点数. 首先读入可以用: Scanner input = new Scanner(System.in); BigInteger a = new input.nextBigInteger(); 这样读还是很方便的 当然还有自己创建: BigInteger a = new BigInteger("1"); int b=1

HDU4927:Series 1(JAVA大数)

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 -

HDU4927 Series 1 高精度

题意:一个数列每一次将它变为  bi = ai+1 - ai    最后数组变为一个数,问你这个数答案为多少. 解题思路:最开始以为这个数一定是在int范围以内的,但是推出了公式就不湿了  公式应该是  C(n,0)*a[n] - C(n,1)*a[n-1] + C(n,2)*a[n-2] ....一直到 a[1], 可以知道这个题系数是二项分布,所以直接用JAVA大数类 a掉的,java大数类有个很恶心的性质,数越大,操作越慢,所以要避免这个情况 解题代码: 1 import java.mat

HDU 4927 Series 1 ( 组合+高精度)

Series 1 大意: 题意不好翻译,英文看懂也不是非常麻烦,就不翻译了. 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

HDU 4927 Series 1 java大数

java mle前会wa 或者 t 这种事我会乱说? import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] a = new BigInteger[3007]; public void work() { int T; T = cin.nextInt(); while (T-- > 0) { int n; n = cin.nextInt(); for (int i = 0;