题意非常easy:

对于长度为n的数。做n-1遍。生成的新数列:

b1=a2-a1   b2=a3-a2  b3=a4-a3

c1=b2-b1   c2=b3-b2

ans=c2-c1

最后推出公式:  为n所在行的杨辉三角

对于例子:

3

1 2 3

ans=1*1-2*2+1*3=0

4

1 5 7 2

ans=-1*1+3*5-3*7+1*2=-5

求杨辉三角每一个数的时候能够优化一下,后一个数由前一个各乘除一次就好了

JAVA用大数秒A。

BUT 不会JAVA  - -#

蛋疼用C++模拟大数。。

#include "stdio.h"
#include "string.h"
int a[3010];
__int64 mark[3010],ans[3010],c[3010];
int Max(int a,int b)
{
if (a<b) return b;else return a;
} void make_mul(int x)
{
int i;
for (i=1;i<=mark[0];i++)
mark[i]*=x; for (i=1;i<=mark[0];i++)
{
mark[i+1]+=mark[i]/1000000;
mark[i]%=1000000;
}
while (mark[mark[0]+1]!=0)
{
mark[0]++;
mark[mark[0]+1]=mark[mark[0]]/1000000;
mark[mark[0]]%=1000000;
} } void make_div(int x)
{
int i;
for (i=mark[0];i>=2;i--)
{
mark[i-1]+=(mark[i]%x)*1000000;
mark[i]/=x;
}
mark[1]/=x;
while (mark[mark[0]]==0) mark[0]--;
} void make_add()
{
int i,op;
if (ans[0]>0)
{
for (i=1;i<=mark[0];i++)
{
ans[i]+=mark[i];
ans[i+1]+=ans[i]/1000000;
ans[i]%=1000000;
}
while (ans[ans[0]+1]!=0)
{
ans[0]++;
ans[ans[0]+1]=ans[ans[0]]/1000000;
ans[ans[0]]%=1000000;
}
return ;
}
else
{
if (-ans[0]>mark[0]) op=-1;
else if (-ans[0]<mark[0]) op=1;
else
{
for (i=mark[0];i>=1;i--)
if (mark[i]>ans[i]) { op=1;break;}
else if (mark[i]<ans[i]) { op=-1; break;} if (i==0)
{
memset(ans,0,sizeof(ans));
ans[0]=1;
return ;
}
} if (op==1)
{
for (i=1;i<=mark[0];i++)
{
ans[i]=mark[i]-ans[i];
if (ans[i]<0)
{
mark[i+1]--;
ans[i]+=1000000;
}
}
ans[0]=-ans[0];
while (ans[ans[0]]==0) ans[0]--;
}
else
{
for (i=1;i<=-ans[0];i++)
{
ans[i]=ans[i]-mark[i];
if (ans[i]<0)
{
ans[i+1]--;
ans[i]+=1000000;
}
}
while (ans[-ans[0]]==0) ans[0]++;
} }
} void make_red()
{
int i,op;
if (ans[0]<0)
{
for (i=1;i<=mark[0];i++)
{
ans[i]+=mark[i];
ans[i+1]+=ans[i]/1000000;
ans[i]%=1000000;
}
while (ans[-ans[0]+1]!=0)
{
ans[0]--;
ans[-ans[0]+1]=ans[-ans[0]]/1000000;
ans[-ans[0]]%=1000000;
}
return ;
}
else
{
if (ans[0]>mark[0]) op=1;
else if (ans[0]<mark[0]) op=-1;
else
{
for (i=mark[0];i>=1;i--)
if (mark[i]>ans[i]) { op=-1;break;}
else if (mark[i]<ans[i]) { op=1; break;} if (i==0)
{
memset(ans,0,sizeof(ans));
ans[0]=1;
return ;
}
} if (op==1)
{
for (i=1;i<=mark[0];i++)
{
ans[i]=ans[i]-mark[i];
if (ans[i]<0)
{
ans[i+1]--;
ans[i]+=1000000;
}
}
while (ans[ans[0]]==0) ans[0]--;
}
else
{
for (i=1;i<=mark[0];i++)
{
ans[i]=mark[i]-ans[i];
if (ans[i]<0)
{
mark[i+1]--;
ans[i]+=1000000;
}
}
ans[0]=mark[0];
while (ans[ans[0]]==0) ans[0]--;
ans[0]=-ans[0];
} } }
int main()
{
int t,n,i,x,y,j,m,op;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]); memset(ans,0,sizeof(ans));
ans[0]=1;
if (i==1)
{
printf("%d\n",a[1]);
continue;
}
memset(mark,0,sizeof(mark));
mark[0]=1;
mark[1]=1;
if (n%2==1) ans[1]=a[1];
else ans[1]=-a[1];
x=1;
y=n-1;
for (i=2;i<=n;i++)
{
make_mul(y);
y--;
make_div(x);
x++;
memcpy(c,mark,sizeof(mark)); make_mul(a[i]);
if (n%2==1)
{
if (i%2==1) make_add();
else make_red();
}
else
{
if (i%2==0) make_add();
else make_red();
}
memcpy(mark,c,sizeof(c)); } if (ans[0]>0)
{
printf("%I64d",ans[ans[0]]);
for (i=ans[0]-1;i>=1;i--)
printf("%06I64d",ans[i]);
printf("\n");
}
else
{
printf("-");
printf("%I64d",ans[-ans[0]]);
for (i=-ans[0]-1;i>=1;i--)
printf("%06I64d",ans[i]);
printf("\n");
} }
return 0;
}

HDU 4927 大数的更多相关文章

  1. HDU 4927 大数运算

    模板很重要 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostrea ...

  2. HDU 4927 Series 1(推理+大数)

    HDU 4927 Series 1 题目链接 题意:给定一个序列,要求不断求差值序列.直到剩一个,输出这个数字 思路:因为有高精度一步.所以要推理一下公式,事实上纸上模拟一下非常easy推出公式就是一 ...

  3. 多校第六场 HDU 4927 JAVA大数类+模拟

    HDU 4927 −ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊.如今对组合算比較什么了-- import java ...

  4. hdu 4927 java求组合数(大数)

    import java.util.Scanner; import java.math.BigInteger; public class Main { private static int [] a = ...

  5. HDU 4927 Series 1(高精度+杨辉三角)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4927 解题报告:对于n,结果如下: C(0,n-1) *A[n] - C(1,n-1) * A[n-1 ...

  6. HDU 4927

    http://acm.hdu.edu.cn/showproblem.php?pid=4927 直接模拟会超时,要在纸上写写推公式 A[n]*C(0,n-1)  - A[n-1]*C(1,n-1) + ...

  7. hdu 4927 组合+公式

    http://acm.hdu.edu.cn/showproblem.php?pid=4927 给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1−ai,直到序列长度 ...

  8. hdu 1002大数(Java)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 5047 大数找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...

随机推荐

  1. 【bzoj2836】魔法树 树链剖分+线段树

    题目描述 输入 输出 样例输入 4 0 1 1 2 2 3 4 Add 1 3 1 Query 0 Query 1 Query 2 样例输出 3 3 2 题解 树剖+线段树模板题,不过为什么写的人这么 ...

  2. Redis主从复制简单介绍

    由于本地环境的使用,所以搭建一个本地的Redis集群,本篇讲解Redis主从复制集群的搭建,使用的平台是Windows,搭建的思路和Linux上基本一致! (精读阅读本篇可能花费您15分钟,略读需5分 ...

  3. bzoj 4292: [PA2015]Równanie

    Description 对于一个正整数n,定义f(n)为它十进制下每一位数字的平方的和.现在给定三个正整数k,a,b,请求出满足a<=n<=b且k*f(n)=n的n的个数.   Input ...

  4. KM算法【带权二分图完美匹配】

    先orz litble--KM算法 为什么要用KM算法 因为有的题丧心病狂卡费用流 KM算法相比于费用流来说,具有更高的效率. 算法流程 我们给每一个点设一个期望值[可行顶标] 对于左边的点来说,就是 ...

  5. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  6. JavaScript 笔记(2) -- 类型转换 & 正则表达 & 变量提升 & 表单验证

    目录:  typeof, null, undefined, valueOf() 类型转换 正则表达式 错误: try, catch, throw 调试工具 变量提升 strict 严格模式 使用误区 ...

  7. 【03】node 之 作用域

    1.什么是作用域 作用域:规定了一个变量和函数可使用的范围,作用域分为两种:全局作用域.局部作用域(函数作用域) 2.NodeJS作用域 NodeJs中一个文件就是一个模块,模块中使用var定义的变量 ...

  8. Static 静态内部类

    Java中的类可以是static吗?答案是可以.在java中我们可以有静态实例变量.静态方法.静态块.类也可以是静态的. java允许我们在一个类里面定义静态类.比如内部类(nested class) ...

  9. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  10. node 监听接口

    var http = require('http'); var mysql = require('mysql'); var connection = mysql.createConnection({ ...