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

HDU 4927 Series 1(推理+大数)

时间:2017-07-05 10:08:55      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:line   div   程序   wrap   tracking   序列   cin   get   数字   

HDU 4927 Series 1

题目链接

题意:给定一个序列,要求不断求差值序列。直到剩一个,输出这个数字

思路:因为有高精度一步。所以要推理一下公式,事实上纸上模拟一下非常easy推出公式就是一个类似杨辉三角的组合数求和,只是奇数位置是加,偶数位置是减,然后高精度过掉

代码:
本人的第一个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[3005];

	int cas, n;
	cas = cin.nextInt();
	while (cas-- != 0) {
	    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++) {
		if (i % 2 == 1) ans = ans.subtract(c.multiply(a[n - i - 1]));
		else ans = ans.add(c.multiply(a[n - i - 1]));

		c = c.multiply(BigInteger.valueOf(n - i - 1));
		c = c.divide(BigInteger.valueOf(i + 1));
	    }
	    System.out.println(ans);
	}
    }
}


HDU 4927 Series 1(推理+大数)

标签:line   div   程序   wrap   tracking   序列   cin   get   数字   

原文地址:http://www.cnblogs.com/llguanli/p/7119383.html

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