Series 1
Problem Description
Let A be an integral series {A
1, A
2, . . . , A
n}.
The zero-order series of A is A itself.
The first-order series of A is {B 1, B 2, . . . , B n-1},where B i = A i+1 - A i.
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.
The zero-order series of A is A itself.
The first-order series of A is {B 1, B 2, . . . , B n-1},where B i = A i+1 - A i.
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 A 1, A 2, . . . , A n. (0<=A i<=10 5)
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 A 1, A 2, . . . , A n. (0<=A i<=10 5)
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
Source
题意:长为n的序列,进行n-1次操作:a[i+1]-a[i],输出最后得到的数
最后结果为:C(0,n-1)*a[n]-C(1,n-1)*a[n-1]+C(2,n-1)*a[n-2]......C(n-1,n-1)*a[1] 符号正负交替
另求解时可利用公式:C(k,n)=C(k-1,n)*(n-k+1)/k
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t>0)
{
t--;
int n=in.nextInt();
BigInteger []a=new BigInteger[30005];
for(int i=0;i<n;i++)
a[i]=in.nextBigInteger();
BigInteger ans=BigInteger.valueOf(0);
BigInteger temp=BigInteger.valueOf(1);
for(int i=0;i<n;i++)
{
ans=ans.add(temp.multiply(a[n-i-1]));
temp=temp.multiply(BigInteger.valueOf(n-i-1)).divide(BigInteger.valueOf(i+1)).multiply(BigInteger.valueOf(-1));
}
System.out.println(ans);
}
in.close();
}
}
附:杨辉三角性质
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
前提:每行端点与结尾的数为1.
-
每个数等于它上方两数之和。
-
每行数字左右对称,由1开始逐渐变大。
-
第n行的数字有n项。
-
第n行数字和为2 n-1。
-
第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同元素中取m-1个元素的组合数。
-
第n行的第m个数和第n-m+1个数相等 ,为 组合数性质之一。
-
每个数字等于上一行的左右两个数字之和。可用此性质写出整个杨辉三角。即第n+1行的第i个数等于第n行的第i-1个数和第i个数之和,这也是组合数的性质之一。即 C(n+1,i)=C(n,i)+C(n,i-1)。
-
(a+b) n的展开式中的各项系数依次对应杨辉三角的第(n+1)行中的每一项。
-
将第2n+1行第1个数,跟第2n+2行第3个数、第2n+3行第5个数……连成一线,这些数的和是第4n+1个斐波那契数;将第2n行第2个 数(n>1),跟第2n-1行第4个数、第2n-2行第6个数……这些数之和是第4n-2个斐波那契数。
-
将各行数字相排列,可得11的n-1(n为行数)次方:1=11^0; 11=11^1; 121=11^2……当n>5时会不符合这一条性质,此时应把第n行的最右面的数字"1"放在个位,然后把左面的一个数字的个位对齐到十位... ...,以此类推,把空位用“0”补齐,然后把所有的数加起来,得到的数正好是11的n-1次方。以n=11为例,第十一行的数为:1,10,45,120,210,252,210,120,45,10,1,结果为 25937424601=11 10。