mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

题目链接:hdu 4927 Series 1

题目大意:给定一个长度为n的序列a,每次生成一个新的序列。长度为n-1。新序列b中bi=ai+1ai,直到序列长度为1.输出最后的数。

解题思路:n最多才3000。ai最大也才1000,貌似不会超int,可是要注意,有些数不止被计算了一次,最多的数被计算了C(15003000),所以肯定要用高精度处理,那么用o(n2)的复杂度肯定就跪了。

事实上对于最后的ans,ans=i=0n1C(in1)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);
        }
    } 
}
版权声明:本文为博主原创文章,未经博主同意不得转载。 举报
  • 本文已收录于下面专栏:

相关文章推荐

[startrelatedarticles]

{relatedtitle}

{relateddes}
[endrelatedarticles] [startrelatedarticlesad1]

{relatedtitle}

{relateddes}
[endrelatedarticlesad1] [startrelatedarticlesad2]
{relateddes}
[endrelatedarticlesad2]
  • 微博
    微信
    QQ
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多仅仅同意输入30个字)

posted on 2017-08-18 12:47  mthoutai  阅读(243)  评论(0编辑  收藏  举报