
该模板不是本人的,但是该是加了个头文件哒。不然在某个oj上编译错误。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
struct bign{
int d[maxn], len;
void clean() { while (len > && !d[len - ]) len--; }
bign() { memset(d, , sizeof(d)); len = ; }
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign operator = (const char* num){
memset(d, , sizeof(d)); len = strlen(num);
for (int i = ; i < len; i++) d[i] = num[len - - i] - '';
clean();
return *this;
}
bign operator = (int num){
char s[]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator + (const bign& b){
bign c = *this; int i;
for (i = ; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > ) c.d[i] %= , c.d[i + ]++;
}
while (c.d[i] > ) c.d[i++] %= , c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i + ;
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
for (i = ; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < ) c.d[i] += , c.d[i + ]--;
}
while (c.d[i] < ) c.d[i++] += , c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
for (j = ; j < b.len; j++) for (i = ; i < len; i++)
c.d[i + j] += d[i] * b.d[j];
for (i = ; i < c.len - ; i++)
c.d[i + ] += c.d[i] / , c.d[i] %= ;
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = ;
for (i = len - ; i >= ; i--)
{
a = a * + d[i];
for (j = ; j < ; j++) if (a < b*(j + )) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = ;
for (i = len - ; i >= ; i--)
{
a = a * + d[i];
for (j = ; j < ; j++) if (a < b*(j + )) break;
a = a - b*j;
}
return a;
}
bign operator += (const bign& b){
*this = *this + b;
return *this;
}
bool operator <(const bign& b) const{
if (len != b.len) return len < b.len;
for (int i = len - ; i >= ; i--)
if (d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{ return b < *this; }
bool operator<=(const bign& b) const{ return !(b < *this); }
bool operator>=(const bign& b) const{ return !(*this < b); }
bool operator!=(const bign& b) const{ return b < *this || *this < b; }
bool operator==(const bign& b) const{ return !(b < *this) && !(b > *this); }
string str() const{
char s[maxn] = {};
for (int i = ; i < len; i++) s[len - - i] = d[i] + '';
return s;
}
}; istream& operator >> (istream& in, bign& x) {
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bign& x)
{
out << x.str();
return out;
}
int main()
{ }