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. Python删除list里面的重复元素的俩种方法

    1.使用set函数  In [116]: a=[1,2,3,2,1,3,4,5,6,5] In [117]: set(a) Out[117]: {1, 2, 3, 4, 5, 6} 2.使用字典函数 ...

  2. 【转】Restful是什么

    REST的概念是什么 维基百科  表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博士论文 ...

  3. Linux 环境变量梳理

    Linux中的环境变量有两种:全局变量和局部变量: 定义.访问.删除局部变量 查看全局变量 可以使用printenv或者env命令来打印所有的全局变量. 访问某一项全局变量,可以使用printenv ...

  4. Memcached 集群架构与memcached-session-manager

    Memcached 集群架构方面的问题_知识库_博客园https://kb.cnblogs.com/page/69074/ memcached-session-manager配置 - 学习中间件调优管 ...

  5. jmeter接口测试------基础笔记

    1.postman发送json格式的post请求,直接放链接 row里面body放请求参数,得到请求结果 2.jmeter请求json时需要注意在请求前创建http信息头管理器,然后信息头添加一条名称 ...

  6. 使用fetch代替ajax请求 post传递方式

    let postData = {a:'b'}; fetch('http://data.xxx.com/Admin/Login/login', { method: 'POST', mode: 'cors ...

  7. MySQL unknown variable 'default-character-set=utf8'的解决

    Windows07 安装了MySQL-server-5.5,直接在命令行输入net start mysql ,启动mysql成功, 然后修改/MySQL Server 5.5/my.ini,增加了de ...

  8. STL 序列容器

    转自时习之 STL中大家最耳熟能详的可能就是容器,容器大致可以分为两类,序列型容器(SequenceContainer)和关联型容器(AssociativeContainer)这里介绍STL中的各种序 ...

  9. jQuery EasyUI window窗口使用实例

    需求:点击[增加]按钮,弹出窗口,并对所有输入项内容进行校验,校验通过就提交给后台的action处理,没有通过校验就弹窗提示.  <!DOCTYPE html> <html> ...

  10. API知识点总结

    一.开发api接口开放给其他人调用的api接口(短信接口,支付宝api) 二.api安全弱点数据窃取(解决加密),数据篡改(解决MD5),数据泄露(爬虫技术)(解决令牌)1.加密(HTTPS传输-收费 ...