上次BC遇到一个大数题目,没有大数模板和不会使用JAVA的同学们GG了,赛后从队友哪里骗出大数模板。2333333,真的炒鸡nice(就是有点长),贴出来分享一下好辣。

 //可以处理字符串前导零
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
using namespace std;
#define maxn 120//大数的个数
class DividedByZeroException {}; class BigInteger
{
private:
vector<char> digits;
bool sign; // true for positive, false for negitive
void trim(); // remove zeros in tail, but if the value is 0, keep only one:)
public:
BigInteger(int); // construct with a int integer
BigInteger(string&) ;
BigInteger();
BigInteger (const BigInteger&);
BigInteger operator=(const BigInteger& op2); BigInteger abs() const;
BigInteger pow(int a); //binary operators friend BigInteger operator+=(BigInteger&,const BigInteger&);
friend BigInteger operator-=(BigInteger&,const BigInteger&);
friend BigInteger operator*=(BigInteger&,const BigInteger&);
friend BigInteger operator/=(BigInteger&,const BigInteger&) throw(DividedByZeroException);
friend BigInteger operator%=(BigInteger&,const BigInteger&) throw(DividedByZeroException); friend BigInteger operator+(const BigInteger&,const BigInteger&);
friend BigInteger operator-(const BigInteger&,const BigInteger&);
friend BigInteger operator*(const BigInteger&,const BigInteger&);
friend BigInteger operator/(const BigInteger&,const BigInteger&) throw(DividedByZeroException);
friend BigInteger operator%(const BigInteger&,const BigInteger&) throw(DividedByZeroException); //uniary operators
friend BigInteger operator-(const BigInteger&); //negative friend BigInteger operator++(BigInteger&); //++v
friend BigInteger operator++(BigInteger&,int); //v++
friend BigInteger operator--(BigInteger&); //--v
friend BigInteger operator--(BigInteger&,int); //v-- friend bool operator>(const BigInteger&,const BigInteger&);
friend bool operator<(const BigInteger&,const BigInteger&);
friend bool operator==(const BigInteger&,const BigInteger&);
friend bool operator!=(const BigInteger&,const BigInteger&);
friend bool operator>=(const BigInteger&,const BigInteger&);
friend bool operator<=(const BigInteger&,const BigInteger&); friend ostream& operator<<(ostream&,const BigInteger&); //print the BigInteger
friend istream& operator>>(istream&, BigInteger&); // input the BigInteger public:
static const BigInteger ZERO;
static const BigInteger ONE;
static const BigInteger TEN;
};
// BigInteger.cpp const BigInteger BigInteger::ZERO=BigInteger();
const BigInteger BigInteger::ONE =BigInteger();
const BigInteger BigInteger::TEN =BigInteger(); BigInteger::BigInteger()
{
sign=true;
} BigInteger::BigInteger(int val) // construct with a int integer
{
if (val >= )
sign = true;
else
{
sign = false;
val *= (-);
}
do
{
digits.push_back( (char)(val%) );
val /= ;
}
while ( val != );
} BigInteger::BigInteger(string& def)
{
sign=true;
for ( string::reverse_iterator iter = def.rbegin() ; iter < def.rend(); iter++)
{
char ch = (*iter);
if (iter == def.rend()-)
{
if ( ch == '+' )
break;
if(ch == '-' )
{
sign = false;
break;
}
}
digits.push_back( (char)((*iter) - '' ) );
}
trim();
} void BigInteger::trim()
{
vector<char>::reverse_iterator iter = digits.rbegin();
while(!digits.empty() && (*iter) == )
{
digits.pop_back();
iter=digits.rbegin();
}
if( digits.size()== )
{
sign = true;
digits.push_back();
}
} BigInteger::BigInteger(const BigInteger& op2)
{
sign = op2.sign;
digits=op2.digits;
} BigInteger BigInteger::operator=(const BigInteger& op2)
{
digits = op2.digits;
sign = op2.sign;
return (*this);
} BigInteger BigInteger::abs() const
{
if(sign) return *this;
else return -(*this);
} BigInteger BigInteger::pow(int a)
{
BigInteger res();
for(int i=; i<a; i++)
res*=(*this);
return res;
} //binary operators
BigInteger operator+=(BigInteger& op1,const BigInteger& op2)
{
if( op1.sign == op2.sign )
{
//只处理相同的符号的情况,异号的情况给-处理
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_add = ; //进位
while ( iter1 != op1.digits.end() && iter2 != op2.digits.end())
{
(*iter1) = (*iter1) + (*iter2) + to_add;
to_add = ((*iter1) > ); // 大于9进一位
(*iter1) = (*iter1) % ;
iter1++;
iter2++;
}
while ( iter1 != op1.digits.end() ) //
{
(*iter1) = (*iter1) + to_add;
to_add = ( (*iter1) > );
(*iter1) %= ;
iter1++;
}
while ( iter2 != op2.digits.end() )
{
char val = (*iter2) + to_add;
to_add = (val > ) ;
val %= ;
op1.digits.push_back(val);
iter2++;
}
if( to_add != )
op1.digits.push_back(to_add);
return op1;
}
else
{
if (op1.sign)
return op1 -= (-op2);
else
return op1= op2 - (-op1);
} } BigInteger operator-=(BigInteger& op1,const BigInteger& op2)
{
if( op1.sign == op2.sign )
{
//只处理相同的符号的情况,异号的情况给+处理
if(op1.sign)
{
if(op1 < op2) // 2 - 3
return op1=-(op2 - op1);
}
else
{
if(-op1 > -op2) // (-3)-(-2) = -(3 - 2)
return op1=-((-op1)-(-op2));
else // (-2)-(-3) = 3 - 2
return op1= (-op2) - (-op1);
}
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin(); char to_substract = ; //借位 while ( iter1 != op1.digits.end() && iter2 != op2.digits.end())
{
(*iter1) = (*iter1) - (*iter2) - to_substract;
to_substract = ;
if( (*iter1) < )
{
to_substract=;
(*iter1) += ;
}
iter1++;
iter2++;
}
while ( iter1 != op1.digits.end() )
{
(*iter1) = (*iter1) - to_substract;
to_substract = ;
if( (*iter1) < )
{
to_substract=;
(*iter1) += ;
}
else break;
iter1++;
}
op1.trim();
return op1;
}
else
{
if (op1 > BigInteger::ZERO)
return op1 += (-op2);
else
return op1 = -(op2 + (-op1));
}
}
BigInteger operator*=(BigInteger& op1,const BigInteger& op2)
{
BigInteger result();
if (op1 == BigInteger::ZERO || op2==BigInteger::ZERO)
result = BigInteger::ZERO;
else
{
vector<char>::const_iterator iter2 = op2.digits.begin();
while( iter2 != op2.digits.end() )
{
if(*iter2 != )
{
deque<char> temp(op1.digits.begin() , op1.digits.end());
char to_add = ;
deque<char>::iterator iter1 = temp.begin();
while( iter1 != temp.end() )
{
(*iter1) *= (*iter2);
(*iter1) += to_add;
to_add = (*iter1) / ;
(*iter1) %= ;
iter1++;
}
if( to_add != )
temp.push_back( to_add );
int num_of_zeros = iter2 - op2.digits.begin();
while( num_of_zeros--)
temp.push_front();
BigInteger temp2;
temp2.digits.insert( temp2.digits.end() , temp.begin() , temp.end() );
temp2.trim();
result = result + temp2;
}
iter2++;
}
result.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
}
op1 = result;
return op1;
} BigInteger operator/=(BigInteger& op1 , const BigInteger& op2 ) throw(DividedByZeroException)
{
if( op2 == BigInteger::ZERO )
throw DividedByZeroException();
BigInteger t1 = op1.abs(), t2 = op2.abs();
if ( t1 < t2 )
{
op1 = BigInteger::ZERO;
return op1;
}
//现在 t1 > t2 > 0
//只需将 t1/t2的结果交给result就可以了
deque<char> temp;
vector<char>::reverse_iterator iter = t1.digits.rbegin(); BigInteger temp2();
while( iter != t1.digits.rend() )
{
temp2 = temp2 * BigInteger::TEN + BigInteger( (int)(*iter) );
char s = ;
while( temp2 >= t2 )
{
temp2 = temp2 - t2;
s = s + ;
}
temp.push_front( s );
iter++;
}
op1.digits.clear();
op1.digits.insert( op1.digits.end() , temp.begin() , temp.end() );
op1.trim();
op1.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
return op1;
} BigInteger operator%=(BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException)
{
return op1 -= ((op1 / op2)*op2);
} BigInteger operator+(const BigInteger& op1,const BigInteger& op2)
{
BigInteger temp(op1);
temp += op2;
return temp;
}
BigInteger operator-(const BigInteger& op1,const BigInteger& op2)
{
BigInteger temp(op1);
temp -= op2;
return temp;
} BigInteger operator*(const BigInteger& op1,const BigInteger& op2)
{
BigInteger temp(op1);
temp *= op2;
return temp; } BigInteger operator/(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException)
{
BigInteger temp(op1);
temp /= op2;
return temp;
} BigInteger operator%(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException)
{
BigInteger temp(op1);
temp %= op2;
return temp;
} //uniary operators
BigInteger operator-(const BigInteger& op) //negative
{
BigInteger temp = BigInteger(op);
temp.sign = !temp.sign;
return temp;
} BigInteger operator++(BigInteger& op) //++v
{
op += BigInteger::ONE;
return op;
} BigInteger operator++(BigInteger& op,int x) //v++
{
BigInteger temp(op);
++op;
return temp;
} BigInteger operator--(BigInteger& op) //--v
{
op -= BigInteger::ONE;
return op;
} BigInteger operator--(BigInteger& op,int x) //v--
{
BigInteger temp(op);
--op;
return temp;
} bool operator<(const BigInteger& op1,const BigInteger& op2)
{
if( op1.sign != op2.sign )
return !op1.sign;
else
{
if(op1.digits.size() != op2.digits.size())
return (op1.sign && op1.digits.size()<op2.digits.size())
|| (!op1.sign && op1.digits.size()>op2.digits.size());
vector<char>::const_reverse_iterator iter1,iter2;
iter1 = op1.digits.rbegin();
iter2 = op2.digits.rbegin();
while( iter1 != op1.digits.rend() )
{
if( op1.sign && *iter1 < *iter2 ) return true;
if( op1.sign && *iter1 > *iter2 ) return false;
if( !op1.sign && *iter1 > *iter2 ) return true;
if( !op1.sign && *iter1 < *iter2 ) return false;
iter1++;
iter2++;
}
return false;
}
}
bool operator==(const BigInteger& op1,const BigInteger& op2)
{
if( op1.sign != op2.sign || op1.digits.size() != op2.digits.size() )
return false;
vector<char>::const_iterator iter1,iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
while( iter1!= op1.digits.end() )
{
if( *iter1 != *iter2 ) return false;
iter1++;
iter2++;
}
return true;
} bool operator!=(const BigInteger& op1,const BigInteger& op2)
{
return !(op1==op2);
} bool operator>=(const BigInteger& op1,const BigInteger& op2)
{
return (op1>op2) || (op1==op2);
} bool operator<=(const BigInteger& op1,const BigInteger& op2)
{
return (op1<op2) || (op1==op2);
} bool operator>(const BigInteger& op1,const BigInteger& op2)
{
return !(op1<=op2);
} ostream& operator<<(ostream& stream,const BigInteger& val) //print the BigInteger
{
if (!val.sign)
stream << "-";
for ( vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++)
stream << (char)((*iter) + '');
return stream;
} istream& operator>>(istream& stream, BigInteger& val)
{
//Input the BigInteger
string str;
stream >> str;
val=BigInteger(str);
return stream;
}
BigInteger a;
//定义一个大数a

在Hdu上要用G++提交,C++提交会CE,亲测!!!!

大数模板 (C ++)的更多相关文章

  1. bignum 大数模板

    今天无意间看到一个很好的大数模板,能算加.减.乘.除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减.我借用 ...

  2. c++大数模板

    自己写的大数模板,参考了小白书上的写法,只是实现了加减乘法,不支持负数,浮点数.. 除法还没写o(╯□╰)o以后再慢慢更吧.. 其实除法我用(xie)的(bu)少(lai),乘法写过fft,这模板还是 ...

  3. hdu 5429 Geometric Progression(存个大数模板)

    Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...

  4. ACM大数模板(支持正负整数)

    之前就保留过简陋的几个用外部数组变量实现的简单大数模板,也没有怎么用过,今天就想着整合封装一下,封装成C++的类,以后需要调用的时候也方便得多. 实现了基本的加减乘除和取模运算的操作符重载,大数除以大 ...

  5. 【集训笔记】【大数模板】特殊的数 【Catalan数】【HDOJ1133【HDOJ1134【HDOJ1130

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3324 http://blog.csdn.net/xymscau/artic ...

  6. hdu1042(大数模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 在网上找了个大数模板方便以后用得到. #include<iostream> #inc ...

  7. 大数模板 poj3982

    1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...

  8. Hdu 4762 网络赛 高精度大数模板+概率

    注意题目中的这句话he put the strawberries on the cake randomly one by one,第一次选择草莓其实有N个可能,以某一个草莓为开头,然后顺序的随机摆放, ...

  9. vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)

    P1447开关灯泡 Accepted 标签:CSC WorkGroup III[显示标签] 描写叙述 一个房间里有n盏灯泡.一開始都是熄着的,有1到n个时刻.每一个时刻i,我们会将i的倍数的灯泡改变状 ...

随机推荐

  1. java设计模式----其他模式

    1.桥接:使用桥接模式不只改变你的实现,也改变你的抽象 优点: 将实现予以解耦,让它和界面之间不再永久绑定 抽象和实现可以独立扩展,不会影响到对方 对于“具体的抽象类”所做的改变,不会影响到客户 用途 ...

  2. sed 常用命令

    删除以ifeq开头的行 sed -i "/^ifeq/d" file 删除空行 sed -i '/^$/d' file

  3. Effective C++ 条款42

    本节条款我们讨论一下class 关键字和typename关键字的不同以及对于模板函数(template function)的影响. 例如以下代码: template<class T> T ...

  4. 不能实现RadioButton默认选择

    当用RadioButton时,希望在程序运行的时候默认一个选项: CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1); //CheckRadioBut ...

  5. axis、xfire、CXF 、JWS

    1.JWS是Java语言对WebService服务的一种实现,用来开发和发布服务.而从服务本身的角度来看JWS服务是没有语言界限的.但是Java语言为Java开发者提供便捷发布和调用WebServic ...

  6. js-easyUI格式化时间

    formatter : function(value, row) { if(value != null){ var date = new Date(value); var y = date.getFu ...

  7. 详解likely和unlikely函数【转】

    本文转载自:http://blog.csdn.net/npy_lp/article/details/7175517 内核源码:Linux-2.6.38.8.tar.bz2 参考文档:http://gc ...

  8. 有关定时器setTimeout()、setInterval()详解

    JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成. setTimeout() setTimeout函数用 ...

  9. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  10. hadoop-3.0.0-alpha4启动

    全部启动或者全部停止(注意:第一次启动需要先格式,以后就不需要格式了,不能多次格式化) 1.启动 [root@master sbin]# pwd /usr/hadoop/hadoop-3.0.0-al ...