模板很重要

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std; #define MAXN 9999
#define MAXSIZE 10
#define DLEN 4 class BigInt
{
private:
int a[]; //可以控制大数的位数
int len; //大数长度
public:
BigInt(){ len = ;memset(a,,sizeof(a)); } //构造函数
BigInt(const int); //将一个int类型的变量转化为大数
BigInt(const char*); //将一个字符串类型的变量转化为大数
BigInt(const BigInt &); //拷贝构造函数
BigInt &operator=(const BigInt &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigInt&); //重载输入运算符
friend ostream& operator<<(ostream&, BigInt&); //重载输出运算符 BigInt operator+(const BigInt &) const; //重载加法运算符,两个大数之间的相加运算
BigInt operator-(const BigInt &) const; //重载减法运算符,两个大数之间的相减运算
BigInt operator*(const BigInt &) const; //重载乘法运算符,两个大数之间的相乘运算
BigInt operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigInt operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigInt & T)const; //大数和另一个大数的大小比较
bool operator<(const BigInt & T) const;
bool operator==(const BigInt & T) const;
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较
bool operator<(const int &t) const;
bool operator==(const int &t) const; void print(); //输出大数
}; bool BigInt::operator==(const BigInt & T) const {
return !(*this > T) && !(T > *this);
}
bool BigInt::operator==(const int &t) const {
BigInt T = BigInt(t);
return *this == T;
}
bool BigInt::operator<(const BigInt & T) const {
return T > *this;
}
bool BigInt::operator<(const int &t) const {
return BigInt(t) > *this;
}
BigInt::BigInt(const int b) //将一个int类型的变量转化为大数
{
int c,d = b;
len = ;
memset(a,,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigInt::BigInt(const char*s) //将一个字符串类型的变量转化为大数
{
int t,k,index,l,i;
memset(a,,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=;
for(i=l-;i>=;i-=DLEN)
{
t=;
k=i-DLEN+;
if(k<)
k=;
for(int j=k;j<=i;j++)
t=t*+s[j]-'';
a[index++]=t;
}
}
BigInt::BigInt(const BigInt & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = T.a[i];
}
BigInt & BigInt::operator=(const BigInt & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigInt & b) //重载输入运算符
{
char ch[MAXSIZE*];
int i = -;
in>>ch;
int l=strlen(ch);
int count=,sum=;
for(i=l-;i>=;)
{
sum = ;
int t=;
for(int j=;j<&&i>=;j++,i--,t*=)
{
sum+=(ch[i]-'')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in; }
ostream& operator<<(ostream& out, BigInt& b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for(i = b.len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << b.a[i];
}
return out;
} BigInt BigInt::operator+(const BigInt & T) const //两个大数之间的相加运算
{
BigInt t(*this);
int i,big; //位数
big = T.len > len ? T.len : len;
for(i = ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -=MAXN+;
}
}
if(t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigInt BigInt::operator-(const BigInt & T) const //两个大数之间的相减运算
{
int i,j,big;
bool flag;
BigInt t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=;
}
else
{
t1=T;
t2=*this;
flag=;
}
big=t1.len;
for(i = ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + ;
while(t1.a[j] == )
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if(flag)
t1.a[big-]=-t1.a[big-];
return t1;
} BigInt BigInt::operator*(const BigInt & T) const //两个大数之间的相乘运算
{
BigInt ret;
int i,j,up;
int temp,temp1;
for(i = ; i < len ; i++)
{
up = ;
for(j = ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if(up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigInt BigInt::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigInt ret;
int i,down = ;
for(i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
int BigInt::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
{
int i,d=;
for (i = len-; i>=; i--)
{
d = ((d * (MAXN+))% b + a[i])% b;
}
return d;
}
BigInt BigInt::operator^(const int & n) const //大数的n次方运算
{
BigInt t,ret();
int i;
if(n<)
exit(-);
if(n==)
return ;
if(n==)
return *this;
int m=n;
while(m>)
{
t=*this;
for( i=;i<<<=m;i<<=)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==)
ret=ret*(*this);
}
return ret;
}
bool BigInt::operator>(const BigInt & T) const //大数和另一个大数的大小比较
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - ;
while(a[ln] == T.a[ln] && ln >= )
ln--;
if(ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigInt::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigInt b(t);
return *this>b;
} void BigInt::print() //输出大数
{
int i;
printf("%d", a[len-]);
for (int i = len-; i >= ; --i) {
printf("%04d", a[i]);
}
puts("");
}
int main()
{
//freopen("1007.in","r",stdin);
int tt,n,a[];
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
BigInt Z(),ans(),tmp;
bool zheng=;
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
for(int i=; i<=n-; i++)
{
if(i>) Z=(Z*(BigInt)(n-i))/i;
if(i%==)
{
tmp=Z*(BigInt)(a[n-i]);
if(zheng)
ans=ans+tmp;
else
{
if(ans<tmp)
{
ans=tmp-ans;
zheng=;
}
else
ans=ans-tmp;
}
}
else
{
tmp=(Z*(BigInt)(a[n-i]));
if(zheng)
{
if(ans<tmp)
{
ans=tmp-ans;
zheng=;
}
else
ans=ans-tmp;
}else
ans=ans+tmp;
}
}
if(!zheng) cout<<'-';
ans.print();
}
return ;
}

HDU 4927 大数运算的更多相关文章

  1. HDU 4927 大数

    题意非常easy: 对于长度为n的数.做n-1遍.生成的新数列: b1=a2-a1   b2=a3-a2  b3=a4-a3 c1=b2-b1   c2=b3-b2 ans=c2-c1 最后推出公式: ...

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

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

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

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

  4. 大数运算(python2)

    偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...

  5. 收藏的一段关于java大数运算的代码

    收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...

  6. [PKU2389]Bull Math (大数运算)

    Description Bulls are so much better at math than the cows. They can multiply huge integers together ...

  7. java 大数运算[转]

    用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...

  8. A+B大数运算

    基础加法大数运算: [https://vjudge.net/problem/HDU-1002] 题目: 输入两个长度不超过1000的整数求出sum. 思路: 由于数字很大不能直接加,用字符串形式输入, ...

  9. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

随机推荐

  1. VBS基础篇 - 动态数组

    VBS中的动态数组需要使用System.Collections.ArrayList '建立动态数组 Dim Arrl : Set Arrl = CreateObject("System.Co ...

  2. Week1 Team Homework #3: 软件工程在北航

    在组内成员的共同努力,我们采访了几个学长学姐,顺利完成任务.反馈信息如下: 平均每周花在这门课上的时间 平均写的代码总行数 学到的最有用的部分 最没用的部分 <软件工程>最应该改进的地方 ...

  3. sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula

    首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())

  4. 【BZOJ】【1013】【JSOI2008】球形空间产生器sphere

    高斯消元 高斯消元模板题 /************************************************************** Problem: 1013 User: Tun ...

  5. MVC4 错误: 检测到有潜在危险的 Request.Form值

    说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止.该值可能指示存在危及应用程序安全的尝试,如跨站点脚本攻击.若要允许页面重写应用程序请求验证设置,请将 httpRuntime  ...

  6. failed creating the Direct3d device--debug

    D3DDEVTYPE_REF 使用REF设备,用软件模拟Direct3D API 照理说是为了让电脑能跑本机不能硬件执行的渲染命令的 但我 pDeviceSettings->d3d9.Devic ...

  7. ssh-add 报错 Could not open a connection to your authentication agent

    ERROR: [root@testcentos01 ~]# ssh-add Could not open a connection to your authentication agent 在shel ...

  8. oracle 笔记

    1.Oracle认证,与其它数据库比较,安装 Oracle安装会自动的生成sys用户和system用户: (1)sys用户是超级用户,具有最高权限,具有sysdba角色,有create databas ...

  9. 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel

    在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...

  10. Unity3D脚本中文系列教程(二)

    原地址:http://dong2008hong.blog.163.com/blog/static/469688272014030347910/ Unity3D脚本中文系列教程(一) .根据名称或标签定 ...