标签:style http color java os io for ar
题目大意:给定一个长度为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 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[3005];
int cas, n;
cas = cin.nextInt();;
for (int k = 0; k < cas; k++) {
n = cin.nextInt();
for (int i = 0; i < n; i++)
a[i] = cin.nextBigInteger();
BigInteger ans = BigInteger.valueOf(0);
BigInteger c = BigInteger.valueOf(1);;
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);
}
}
}
hdu 4927 Series 1(组合+公式),布布扣,bubuko.com
标签:style http color java os io for ar
原文地址:http://blog.csdn.net/keshuai19940722/article/details/38422957