【大数问题】 HDOJ 4927 Series 1

大数+组合数。。。

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

public class Main
{
	public static void main(String[] args)
	{
		
		Scanner scanner = new Scanner(System.in);
		BigInteger f[] = new BigInteger[3005];
		BigInteger num[] = new BigInteger[3005];
		BigInteger ans, tmp;
		int t = scanner.nextInt();
		while((t--) != 0) {
			int n = scanner.nextInt();
			f[0] = BigInteger.ONE;
			for(int i = 1; i < n; i++) {
				tmp = BigInteger.valueOf(n-i);
				f[i] = f[i-1].multiply(tmp);
				tmp = BigInteger.valueOf(i);
				f[i] = f[i].divide(tmp);
			}
			ans = BigInteger.ZERO;
			for(int i = 0; i < n; i++) num[i] = scanner.nextBigInteger();
			int flag = 0;
			for(int i = n-1; i >= 0; i--) {
				if(flag == 0) {
					ans = ans.add(f[i].multiply(num[i]));
					flag = 1;
				}
				else {
					ans = ans.subtract(f[i].multiply(num[i]));
					flag = 0;
				}
			}
			System.out.println(ans.toString());
		}
	}
}


你可能感兴趣的:(HDU)