BUPT2017 wintertraining(15) #5I

题意

输出序列A[1..n]的第n-1阶差分(一个整数)。

题解

观察可知答案就是

\[\sum_{i=0}^{n-1} {(-1)^{i}C_n^{i} A_{n-i}}
\]

需要用大整数。

代码

Java代码

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*; public class Main{
public static void main(String[] argc){
Scanner cin = new Scanner (new BufferedInputStream(System.in));
int T = cin.nextInt();
while(T>0){
--T;
int n = cin.nextInt();
BigInteger a, t, x;
a = BigInteger.ZERO;
t = BigInteger.ONE;
for (int i = 1; i <= n; ++i){
BigInteger b = BigInteger.ONE;
if (i >= 2){
t = t.multiply(BigInteger.valueOf(n-i+1));
t = t.divide(BigInteger.valueOf(i-1));
}
x = cin.nextBigInteger();
b = b.multiply(t);
b = b.multiply(x);
if (1 == (n - i) % 2) a = a.subtract(b);
else a = a.add(b);
}
System.out.println(a);
}
}
}

C++代码

#include <cstdio>
#include <cstring>
using namespace std;
#define N 3005
#define base 10000
struct Big{
int d[1000];
int k,l;
Big(){l=k=0;}
Big(int d){input(d);}
void input(int n){
memset(d,0,sizeof d);
k=0;
if(n==0) l=1;
else for(l=0;n;n/=base)d[l++]=n%base;
}
void output()const{
if(k&&l&&d[l-1])printf("-");
printf("%d",d[l-1]);
for(int i=l-2;i>=0;i--)printf("%04d",d[i]);
}
bool operator <(const Big &b)const{
int i=l-1,j=b.l-1;
if(i!=j)return i<j;
while(i>=0&&d[i]==b.d[j]){i--;j--;}
return d[i]<b.d[j];
}
Big operator * (const Big &b)const{
Big c;
memset(c.d,0,sizeof c.d);
for(int i=0;i<l;i++)
for(int j=0;j<b.l;j++){
c.d[i+j]+=d[i]*b.d[j];
if(c.d[i+j]>=base){
c.d[i+j+1]+=c.d[i+j]/base;
c.d[i+j]%=base;
}
}
c.l=l+b.l;
if(c.l>1&&!c.d[c.l-1])c.l--;
return c;
}
Big operator * (int b)const{
Big c;
c.input(b);
return *this*c;
}
Big operator / (int b)const{
Big c;
int i,k=0;
c.l=l;
for(i=l-1;i>=0;i--){
c.d[i]=(k+d[i])/b;
k=(k+d[i])%b*base;
}
while(c.l>1&&!c.d[c.l-1])c.l--;
return c;
}
Big operator + (const Big &b)const{
Big c=*this;
int k=0;
for(int i=0;i<b.l||i<l;i++){
if(i<b.l)c.d[i]+=b.d[i];
c.d[i]+=k;
k=0;
if(c.d[i]>=base){
c.d[i]-=base;
k=1;
}
}
if(b.l>l)c.l=b.l;
if(k)c.d[c.l++]=1;
return c;
}
Big operator - (const Big &b)const{
Big c=*this;
for(int i=0;i<b.l;i++){
c.d[i]-=b.d[i];
if(c.d[i]<0){
c.d[i]+=base;
c.d[i+1]--;
}
}
while(c.l>1&&!c.d[c.l-1])c.l--;
return c;
}
}a[N];
int T,n;
void add(Big &a,Big b){
if(a.k^b.k){
if(b<a) a=a-b;
else a=b-a,a.k=b.k;
}else a=a+b;
}
void sub(Big &a,Big b){
if(a.k^b.k) a=a+b;
else if(b<a) a=a-b;
else a=b-a,a.k^=1;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1,d;i<=n;i++){
scanf("%d",&d);
a[i].input(d);
}
Big c(1);
int k=n%2;
Big ans=a[1];
if(!k)ans.k=1;
for(int i=1;i<n;i++){
c=c*(n-i)/i;
k^=1;
Big t=c*a[i+1];
if(k) add(ans,t);
else sub(ans,t);
}
ans.output();
puts("");
}
}

相关题目 Series 2

【HDU - 4927】Series 1的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. 使用Request+正则抓取猫眼电影(常见问题)

    目前使用Request+正则表达式,爬取猫眼电影top100的例子很多,就不再具体阐述过程! 完整代码github:https://github.com/connordb/Top-100 总结一下,容 ...

  2. 20分钟 看图手写的table

    <html><body><table width="100%" border="1" cellspacing="0&qu ...

  3. asp.net mvc5 action多个参数

    需要完成http://site.com/user/add/1/2这样的url解析 使用action的参数直接获取数据的方式 Action声明如下 ) { ViewBag.clubID = id; ) ...

  4. Git之项目使用

    现在最为盛行的版本控制器,非git莫属了, 那就看看在项目中我们是如何使用它的吧 一. 在已经存在秘钥对的情况下,我们需要在本地进行相关配置 git config --global user.name ...

  5. 如何使用RSS

    (转载: http://www.ruanyifeng.com/blog/2006/01/rss.html) 一. 自从我发现很多人不知道什么是RSS以后,我就一直想向大家介绍它,因为它太有用了,将来会 ...

  6. 移动端tap事件,消除300毫秒延迟

    引用这个之前,要讲一下首先我是用了webpack技术,所以你的项目如果没有用到这个的话,最好不要用这个技术,当然想用也可以,改下代码也可以用. 下面的代码直接复制就可以用啦. ( function(e ...

  7. spring核心思想:IOC(控制反转)和DI(依赖注入)

    Spring有三大核心思想,分别是控制反转(IOC,Inversion Of Controller),依赖注入(DI,Dependency Injection)和面向切面编程(AOP,Aspect O ...

  8. ResultHandler的用法

    ResultHandler,顾名思义,对返回的结果进行处理,最终得到自己想要的数据格式或类型.也就是说,可以自定义返回类型.下面通过一个例子讲解它的使用方法: 创建Goods实体类: public c ...

  9. Flutter上拉加载下拉刷新---flutter_easyrefresh

    前言 Flutter默认不支持上拉加载,下拉刷新也仅仅支持Material的一种样式.Android开发使用过SmartRefreshLayout的小伙伴都知道这是一个强大的刷新UI库,集成了很多出色 ...

  10. Spark join连接

    内链接