【HDU - 4927】Series 1
BUPT2017 wintertraining(15) #5I
题意
输出序列A[1..n]的第n-1阶差分(一个整数)。
题解
观察可知答案就是
\]
需要用大整数。
代码
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("");
}
}
【HDU - 4927】Series 1的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- Node.js api接口和SQL数据库关联
数据库表创建 服务器环境配置.连接 .操作.数据库 API接口 原则:
- Liunx 简单的命令说明
cd命令在linux中用来切换或者进入目录,路径还分为相对路径和绝对路径 cd 命令:切换当前目录至其他目录 cd /:加上斜杠表示是进入到根目录. pwd命令:查看当前路径. ()cd 进入用户主目 ...
- 【转】Restful是什么
REST的概念是什么 维基百科 表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博士论文 ...
- How to Configure Email Notification in Jenkins
How to Configure Email Notification in Jenkins? - The Official 360logica Bloghttps://www.360logica.c ...
- web安全测试排查
漏洞排查思路: 1.上传漏洞 如果看到:选择你要上传的文件 [重新上传]或者出现“请登陆后使用”,80%就有漏洞了! 有时上传不一定会成功,这是因为Cookies不一样.我们就要用WSockExper ...
- C# Note15:设置Window图标的正确方式
Windows Presentation Foundation(WPF)独立应用程序有两种类型的图标: 一个程序集(assembly) 图标,通过在应用程序的项目构建文件中使用<Applicat ...
- 剑指offer(10)
题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 思路: 如果忽略题目中 ...
- freemarker 简单操作
操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end end: 截取子串的长度,end必须大于 ...
- Linux基础学习(16)--备份与恢复
第十六章——备份与恢复 一.备份概述 1.Linux系统需要备份的数据: 2.备份策略: 二.dump和restore命令 1.dump命令: 2.restore命令:
- python学习笔记(10)--组合数据类型(序列类型)
序列是具有先后关系的一组数据,是一维元素向量,元素类型可以不同,类似数学元素序列,元素间由序号引导,通过下标访问序列的特定元素.序列类型是一个基类类型,字符串类型,元祖类型,列表类型都属于序列类型. ...