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 ...
随机推荐
- Bluetooth
Android provides a default Bluetooth stack, BlueDroid, that is divided into two layers: The Bluetoot ...
- 清除SQL Server执行计划
有时需要调试SQL语句的性能, 需要不断的执行SQL语句, 可是多次执行同一条语句的时候,SQL Server 会缓存表的数据,结果就测不出来 实际的 SQL 的性能 用以下SQL可以清除缓存数据 D ...
- MVC与WebForm的一些区别
MVC与WebForm的一些区别 它们都是ASP.NET WEB开发的两种方式 .但是他们也是有一些不同.做个小结. 1.MVC是没有服务器端控件这么一说的,也就是没有viewstate,也就不会产生 ...
- 3244: [Noi2013]树的计数 - BZOJ
Description 我们知道一棵有根树可以进行深度优先遍历(DFS)以及广度优先遍历(BFS)来生成这棵树的DFS序以及BFS序.两棵不同的树的DFS序有可能相同,并且它们的BFS序也有可能相同, ...
- python 函数默认值的小坑啊
import datetime import time def test(day=datetime.datetime.now()): print day while True: test() time ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads
题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ...
- JQuery图片延迟加载插件,动态获取图片长宽尺寸
以前的网站带宽小,没有特别多的大图,现在不同了,各种图片网站如同雨后春笋层出不穷.服务器是抗住了,但是客户端就有意见了,太多的图片必然导致页面加载缓慢,特别是有些table结构的站点更是如此.能否让图 ...
- 【BZOJ】【1412】【ZJOI2009】狼和羊的故事
网络流/最小割 一开始我是将羊的区域看作连通块,狼的区域看作另一种连通块,S向每个羊连通块连一条无穷边,每个狼连通块向T连一条无穷边,连通块内部互相都是无穷边.其余是四连通的流量为1的边……然后WA了 ...
- linux服务器重启服务命令说明文档
(前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...
- zabbix3.0 安装方法,一键实现短信、电话、微信、APP 告警
引言 免费开源监控工具 Zabbix 因其强大的监控功能得到各大互联网公司的广泛认可,具体功能不再详细介绍,在之前发布的 Zabbix 2.4.1 安装及微信短信提醒已经做了详细介绍,本篇主要对 Za ...