HDU 4927 大数运算
模板很重要
#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 大数运算的更多相关文章
- HDU 4927 大数
题意非常easy: 对于长度为n的数.做n-1遍.生成的新数列: b1=a2-a1 b2=a3-a2 b3=a4-a3 c1=b2-b1 c2=b3-b2 ans=c2-c1 最后推出公式: ...
- HDU 4927 Series 1(推理+大数)
HDU 4927 Series 1 题目链接 题意:给定一个序列,要求不断求差值序列.直到剩一个,输出这个数字 思路:因为有高精度一步.所以要推理一下公式,事实上纸上模拟一下非常easy推出公式就是一 ...
- 多校第六场 HDU 4927 JAVA大数类+模拟
HDU 4927 −ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊.如今对组合算比較什么了-- import java ...
- 大数运算(python2)
偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- [PKU2389]Bull Math (大数运算)
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- java 大数运算[转]
用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...
- A+B大数运算
基础加法大数运算: [https://vjudge.net/problem/HDU-1002] 题目: 输入两个长度不超过1000的整数求出sum. 思路: 由于数字很大不能直接加,用字符串形式输入, ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
随机推荐
- Nginx + Tomcat 动静分离实现负载均衡(转)
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...
- php 安全处理方案
Safe::mysqlSafe(); sql注入,升级5.3.6以上版本php 方案一:将所有请求中所有数据(get/post/cookie)实现mysql_escape_string进行安全处理. ...
- hibernate中文乱码问题
在学习hibernate的过程中,发现在往mysql数据库中插入数据的时候会报错. <property name="hibernate.connection.url"> ...
- firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object
今天在测试系统时,一个很正常的功能在firefox下报错,经过验证在ie和chrome浏览器中功能这个正常. 调试后发现: 请求比其他请求的特殊点在于同步请求. 经过firefox的控制台上测 ...
- 原码 & 反码 & 补码 & 详解
本篇文章讲解了计算机的原码, 反码和补码. 并且进行了深入探求了为何要使用反码和补码, 以及更进一步的论证了为何可以用反码, 补码的加法计算原码的减法. 论证部分如有不对的地方请各位牛人帮忙指正! 希 ...
- Unity3D脚本中文系列教程(一)
原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403115643431/?suggestedreading&wumii ...
- Spring @ Component 的作用
1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...
- jsp中文件下载的实现
jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>.但是这样服务器上的目录资源会直 ...
- eclipse配置mahout
1.在elcipse上建立一个java project 项目名:mymahout 2.建立libs文件夹,在mahout 0.9的lib文件夹下找到一下java包 其中log4j.properties ...
- 学习笔记--Git安装 创建版本库 图文详解
一.Git下载 在Windows上安装git,一般为msysgit,官网地址:http://git-scm.com/ 我下载的是Git-1.9.2-preview20140411.exe 二.Git安 ...