14 #include "utility/strutility.hpp"
17 return len ==1 &&
d[0]==0;
49 d.push_back(num % 10000);
62 for(size_t i=0;i<s.size();++i)
65 else if (isdigit(s[i]))
70 throw std::runtime_error(s+
" is not a valid BigInt");
72 throw std::runtime_error(s+
" is not a valid BigInt");
73 while (beg < s.size() && s[beg]==
'0')
81 int pos = s.size() -1;
86 if (pos-2 >=beg) cur += 100*
char2int(s[pos-2]
);
87 if (pos-3 >=beg) cur += 1000*
char2int(s[pos-3]
);
103 template<
typename CompareFunc>
106 CompareFunc com = CompareFunc();
108 for (
int i=a
.len-1; i>=0; --i)
109 if (a
.d[i] != b
.d[i])
110 return com(a
.d[i],b
.d[i]);
141 for (
int i=0; i<
len; ++i)
142 if (
d[i]!=b
.d[i])
return false;
148 return !(*
this == b);
153 return *
this >b || *
this==b;
158 return *
this<b || *
this==b;
163 size_t oldsize =
d.size();
167 for (size_t i=0; i<
len; ++i)
169 d[i] += (i<b
.len ? b
.d[i] : 0) + jw;
186 for (size_t i=0; i<
len; ++i)
188 d[i] -= (i<b
.len ? b
.d[i] : 0) + jw;
197 while (
len>1 &&
d[len-1]==0) --
len;
205 if (
isZero()) { *
this=b;
return *
this;}
248 std::ostream& operator <<(std::ostream& o ,
const BigInt& b)
251 for (
int i=b
.len-1; i>=0; --i)
255 if (b
.d[i]<1000) o<<
'0';
256 if (b
.d[i]<100) o<<
'0';
257 if (b
.d[i]<10) o<<
'0';
282 std::vector<int32_t> &v= ans
.d;
284 for (size_t i=0; i<
len; ++i)
285 for (size_t j=0; j<b
.len; ++j)
286 v[i+j] +=
d[i] * b
.d[j];
288 for (size_t i=0; i<
len + b
.len; ++i)
296 while (ans
.len>1 && v[ans.len-1]==0) --ans
.len;
317 if (b
.isZero())
throw std::runtime_error(
"division to zero");
322 for(
int i=
len-1; i>=0; --i)
327 int32_t l=0, r=10000;
328 int32_t mid = (l+r)/2;
345 while (
len>1 &&
d[len-1]==0) --
len;
BigInt & operator/=(const BigInt &b)
std::pair< BigInt &, BigInt > divandmod(const BigInt &b)
BigInt & assign(long long num)
BigInt & operator%=(const BigInt &b)
BigInt(const std::string &s)
BigInt operator*(const BigInt &b) const
BigInt & operator-=(const BigInt &b)
bool operator>(const BigInt &b) const
bool operator<=(const BigInt &b) const
bool rawGreater(const BigInt &b) const
BigInt & rawPlus(const BigInt &b)
bool operator<(const BigInt &b) const
BigInt & operator+=(const BigInt &b)
bool operator>=(const BigInt &b) const
BigInt operator/(const BigInt &b) const
BigInt & operator*=(const BigInt &b)
BigInt & setSign(const bool sign)
BigInt & rawMinus(const BigInt &b)
bool operator!=(const BigInt &b) const
bool rawSmaller(const BigInt &b) const
BigInt operator%(const BigInt &b) const
std::istream & operator>>(std::istream &i, BigInt &b)
bool rawCompare(const BigInt &a, const BigInt &b)
BigInt operator+(const BigInt &b) const
bool operator==(const BigInt &b) const
BigInt operator-(const BigInt &b) const
BigInt & assign(const std::string &s)