代码
class Int {
static const int BASE = 100000000;
static const int WIDTH = 8;
public:
vector<int> s;
Int& clean() {
while (!s.back() && s.size() > 1) s.pop_back();
return *this;
}
Int(unsigned long long num = 0) {*this = num;}
Int(string s) {*this = s;}
Int& operator=(long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while (num > 0);
return *this;
}
Int& operator=(const string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len; i++) {
int end = str.length() - i * WIDTH;
int start = max(0, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return this->clean();
}
Int operator+(const Int& b) const {
Int c;
c.s.clear();
for (int i = 0, g = 0;; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size()) x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x % BASE);
g = x / BASE;
}
return c;
}
Int operator-(const Int& b) const {
assert(b <= *this);
Int c;
c.s.clear();
for (int i = 0, g = 0;; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = s[i] + g;
if (i < b.s.size()) x -= b.s[i];
if (x < 0) {
g = -1;
x += BASE;
} else g = 0;
c.s.push_back(x);
}
return c.clean();
}
Int operator*(const Int& b) const {
int i, j;
unsigned long long g;
vector<unsigned long long> v(s.size() + b.s.size(), 0);
Int c;
c.s.clear();
for (i = 0; i < s.size(); i++)
for (j = 0; j < b.s.size(); j++)
v[i + j] += (unsigned long long)(s[i]) * b.s[j];
for (i = 0, g = 0;; i++) {
if (g == 0 && i >= v.size()) break;
unsigned long long x = v[i] + g;
c.s.push_back(x % BASE);
g = x / BASE;
}
return c.clean();
}
Int operator/(const Int& b) const {
assert(b > 0);
Int c = *this, m;
for (int i = s.size() - 1; i > -1; i--) {
m = m * BASE + s[i];
c.s[i] = bsearch(b, m);
m -= b * c.s[i];
}
return c.clean();
}
Int operator%(const Int& b) const {
Int c = *this;
Int m;
for (int i = s.size() - 1; i > -1; i--) {
m = m * BASE + s[i];
c.s[i] = bsearch(b, m);
m -= b * c.s[i];
}
return m;
}
int bsearch(const Int& b, const Int& m) const {
int L = 0, R = BASE - 1, x;
while (1) {
x = (L + R) >> 1;
if (b * x <= m) {
if (b * (x + 1) > m) return x;
else L = x;
} else R = x;
}
}
Int& operator+=(const Int& b) {
*this = *this + b;
return *this;
}
Int& operator-=(const Int& b) {
*this = *this - b;
return *this;
}
Int& operator*=(const Int& b) {
*this = *this * b;
return *this;
}
Int& operator/=(const Int& b) {
*this = *this / b;
return *this;
}
Int& operator%=(const Int& b) {
*this = *this % b;
return *this;
}
bool operator<(const Int& b) const {
if (s.size() != b.s.size()) return s.size() < b.s.size();
for (int i = s.size() - 1; i; i--)
if (s[i] != b.s[i]) return s[i] < b.s[i];
return s[0] < b.s[0];
}
bool operator>(const Int& b) const {return b < *this;}
bool operator<=(const Int& b) const {return !(b < *this);}
bool operator>=(const Int& b) const {return !(*this < b);}
bool operator!=(const Int& b) const {return b < *this || *this < b;}
bool operator==(const Int& b) const {return !(b < *this) && !(b > *this);}
};
ostream& operator<<(ostream& out, const Int& x) {
out << x.s.back();
for (int i = x.s.size() - 2; i > -1; i--) {
char buf[20];
sprintf(buf, "%08d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) out << buf[j];
}
return out;
}
istream& operator>>(istream& in, Int& x) {
string s;
if (!(in >> s)) return in;
x = s;
return in;
}使用方法
使用之前,一定要加上 #include <cassert> 头文件,而且它不在万能头中。
定义:Int a,b;
输入:cin>>a>>b;
输出:cout<<a<<' '<<b<<endl;
其余运算与普通 int 相同。
使用减法时,注意结果不能为负数。
使用除法时,注意除数为正数。
若不遵守,那么程序将触发 assert 警告机制,无法正常运行。