【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 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- Day9 Python基础之函数基础(七)
参考链接:https://www.cnblogs.com/yuanchenqi/articles/5828233.html 1.函数的定义 定义: 函数是指将一组语句的集合通过一个函数名封装起来,要想 ...
- HTTPS的SSL证书配置
SSL证书 TOMCAT7.0部署_百度经验https://jingyan.baidu.com/article/7082dc1c65066be40a89bda8.html SSL证书安装指引 - 青春 ...
- MySQL之慢查询日志和通用查询
MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1.通用查询日志:记录建立的客户端连接和执行的语句. 2.慢查 ...
- linux之常见错误
在日常开发中,尤其是在Linux中进行操作的时候,经常会碰到各种各样的错误.记录一下,熟能生巧,慢慢参透linux的奥秘 1) 在安装ssl证书的时候,发生certbot命令无法使用的情况 解决方案: ...
- ShowDoc上手
ShowDoc是什么 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! 程序员都很希望别人能写技术文档,而自己却 ...
- opencv2\flann\matrix.h(69): error C2059: 语法错误:“,”
在提示错误的matrix.h头文件中,修改一下,在free前加上_ ,即FLANN_DEPRECATED void _free() .
- [转帖]TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大
TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大 https://www.jianshu.com/p/efe44d4a7501?utm_source=oschina-app 又拍 ...
- 从git中删除 .idea 目录
将.idea目录加入ignore清单: $ echo '.idea' >> .gitignore 从git中删除idea: $ git rm —cached -r .idea 3 将. ...
- Java并发编程之ThreadGroup
ThreadGroup是Java提供的一种对线程进行分组管理的手段,可以对所有线程以组为单位进行操作,如设置优先级.守护线程等. 线程组也有父子的概念,如下图: 线程组的创建 public class ...
- 老男孩python学习自修第七天【包与模块】
1.如何导入 from package import module module.function() 常用魔术方法 __init__.py 如果某个文件夹下面有该文件,则该文件夹是一个包,否则只是一 ...