简介
原本使用的是网上封装的结构体,后来遇到的题目越多,发现单纯的高精对高精的封装是不够的,有时候需要高精乘低精,高精除低精的操作,因为这样的操作时间复杂度更低。还有对于负数情况的完善。
因此我重新完善了自己的高精度模板,支持负数,高精对高精运算,高精对低精运算。
我的代码的验证题目是P1932 A+B A-B A*B A/B A%B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
过的非常勉强,关闭输入输出缓冲流并且开启了O2优化。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 20005;
struct bign{
int d[maxn], len, neg;
void clean() { while(len > 1 && !d[len-1]) len--; }
bign() { memset(d, 0, sizeof(d)); len = 1; neg=0;}
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign(string num) {*this = num; }
bign operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num); neg = 0;
if (num[0] == '-') neg = 1;
for(int i = neg; i < len; i++) d[i-neg] = num[len-1-i+neg] - '0';
if (neg) len--;
clean();
return *this;
}
bign operator = (const string num){
memset(d, 0, sizeof(d)); len = num.size(); neg=0;
if (num[0] == '-') neg = 1;
for(int i = neg; i < len; i++) d[i-neg] = num[len-1-i+neg] - '0';
if (neg) len--;
clean();
return *this;
}
bign operator = (int num){
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign abs(bign& b){
b.neg=0;
return b;
}
bign set_neg(bign& b){
b.neg = 1;
return b;
}
bign add (bign c, bign b) {
int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bign sub (bign c, bign b) {
int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bign operator + (const bign& b){
bign c = *this; int i;
bign d = b;
if (c.neg == 0 && b.neg == 0) c = add(c, d);
else if (c.neg && b.neg) {c = abs(c); d = abs(d); c = add(c, d); c = set_neg(c); }
else if (c.neg) { abs(c);
if (c > b) {c = sub(c, b); set_neg(c); }
else { c = sub(b, c); }
} else { abs(d);
if (c > d) {c = sub(c, d);}
else { c = sub(b, c); set_neg(c); }
}
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
bign d = b;
if (c.neg == 0 && d.neg == 0) {
if (c > d) {
c = sub(c, d);
} else {
c = sub(d, c);
set_neg(c);
}
} else if (c.neg && d.neg) { abs(c); abs(d); c = add(c, d); set_neg(c); }
else if (c.neg) {
abs(c);
c = add(c, d);
set_neg(c);
} else {
abs(d);
c = add(c, d);
}
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
c.neg = neg != b.neg;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bign operator * (const int& x)const{
bign c;
if ((x < 0 && neg == 0) || (x >= 0 && neg == 1)) c.neg = 1;
else c.neg = 0;
int xx = x;
if (xx < 0) xx = -xx;
int t = 0, i = 0;
for (; i < len; i++) {
c.d[c.len - 1] = xx * d[i] + t;
t = (c.d[i] - (c.d[i] % 10)) / 10;
c.d[i] = c.d[i] % 10;
c.len++;
}
while (t) {
c.d[c.len - 1] = t % 10, t/= 10;
c.len++;
}
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = 0;
c.neg = neg != b.neg;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator / (const int& x){
bign c;
c.len = len;
if ((x < 0 && neg == 0) || (x >= 0 && neg == 1)) c.neg = 1;
else c.neg = 0;
int xx = x;
if (xx < 0) xx = -xx;
int t = 0;
for (int i = len - 1; i >= 0; i--)
{
t = t * 10 + d[i];
c.d[i] = t / x;
t = t % x;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = 0;
if (neg) a.neg = 1;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) 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-1; i >= 0; 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]={};
if (neg) s[0] = '-';
for(int i = 0; i < len; i++) s[len-1-i+neg] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bign& x)
{
string s;
in >> s;
x = s;
return in;
}
ostream& operator << (ostream& out, const bign& x)
{
out << x.str();
return out;
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
bign a, b;
cin >> a >> b;
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;
return 0;
}