高精板子MLE求调
  • 板块灌水区
  • 楼主stripe_python
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/7/18 11:42
  • 上次更新2023/11/3 09:09:56
查看原帖
高精板子MLE求调
928879
stripe_python楼主2023/7/18 11:42

rt,加上SignedInteger就会MLE,用UnsignedInteger就不会出问题。

题目链接: P1932

#include <bits/stdc++.h>
#define N 10005
using namespace std;

#define DISABLE_FFT

#ifndef BIG_INTEGER_H
#define BIG_INTEGER_H

class UnsignedInteger {
#define delzero while (c.s[c.s[0]] == 0 && c.s[0] > 1) c.s[0]--
	
private:
	static const int BIT = 8;
	static const int MOD = pow(10, BIT);
	
	UnsignedInteger init() {
		memset(s, 0, sizeof(s));
		s[0] = 1;
		return *this;
	}
	
	UnsignedInteger init(const char* num) {
		memset(s, 0, sizeof(s));
		int n = strlen(num);
		s[0] = 0;
		for (int i = n - 1; i >= 0; i -= BIT) {
			s[0]++;
			long long w = 1;
			for (int j = i; j > i - BIT && j >= 0; j--) {
				s[s[0]] += (num[j] ^ 48) * w;
				w = (w << 1) + (w << 3);
			}
		}
		return *this;
	}
	
	UnsignedInteger init(const unsigned long long num) {
		char a[N];
		sprintf(a, "%lld", num);
		return init(num);
	}
	
	int compare(const UnsignedInteger& a) {
		int up = max(s[0], a.s[0]);
		for (int i = 0; i < up; i++) {
			long long d1 = s[up - i], d2 = a.s[up - i];
			if (d1 != d2) {
				return d1 > d2 ? 1 : -1;
			}
		}
		return 0;
	}
	
public:
	long long s[N];
	
	UnsignedInteger() {init();}
	UnsignedInteger(const int num) {init((long long) num);}
	UnsignedInteger(const long long num) {init(num);}
	UnsignedInteger(const char* num) {init(num);}
	
	UnsignedInteger operator = (const int num) {return init((long long) num);}
	UnsignedInteger operator = (const long long num) {return init(num);}
	UnsignedInteger operator = (const char* num) {return init(num);}

	bool operator == (const UnsignedInteger& a) {return compare(a) == 0;}
	bool operator != (const UnsignedInteger& a) {return compare(a) != 0;}
	bool operator > (const UnsignedInteger& a) {return compare(a) == 1;}
	bool operator < (const UnsignedInteger& a) {return compare(a) == -1;}
	bool operator >= (const UnsignedInteger& a) {return compare(a) >= 0;}
	bool operator <= (const UnsignedInteger& a) {return compare(a) <= 0;}

	UnsignedInteger operator + (const UnsignedInteger& a) const {
		UnsignedInteger c;
		int x = 0;
		c.s[0] = max(a.s[0], s[0]) + 1;
		for (int i = 1; i <= c.s[0]; i++) {
			c.s[i] = a.s[i] + s[i] + x;
			x = c.s[i] / MOD;
			c.s[i] %= MOD;
		}
		delzero;
		return c;
	}
	UnsignedInteger operator += (const UnsignedInteger &a) {
		return *this = *this + a;
	}
	
	UnsignedInteger operator - (const UnsignedInteger& a) const {
		UnsignedInteger c;
		c.s[0] = max(a.s[0], s[0]) + 1;
		for (int i = 1; i <= c.s[0]; i++) {
			c.s[i] += s[i] - a.s[i];
			if (c.s[i] < 0) {
				c.s[i] += MOD;
				c.s[i + 1]--;
			}
		}
		delzero;
		return c;
	}
	UnsignedInteger operator -= (const UnsignedInteger &a) {
		return *this = *this - a;
	}
	
	UnsignedInteger operator * (const UnsignedInteger& a) {
		long long digit = a.s[0] + s[0];
#ifndef DISABLE_FFT
		if (digit >= 7e7 || digit < 0) return fft_mul(a);
#endif  // DISABLE_FFT
		
		UnsignedInteger c;
		c.s[0] = digit + 1;
		for (int i = 1; i <= s[0]; i++) {
			int x = 0;
			for (int j = 1; j <= a.s[0]; j++) {
				c.s[i + j - 1] += s[i] * a.s[j] + x;
				x = c.s[i + j - 1] / MOD;
				c.s[i + j - 1] %= MOD;
			}
			c.s[i + a.s[0]] = x;
		}
		while (c.s[c.s[0]] > 0) c.s[0]++;
		delzero;
		return c;
	}
	UnsignedInteger operator *= (const UnsignedInteger &a) {
		return *this = *this * a;
	}
	
	UnsignedInteger operator << (const long long& a) const {
		UnsignedInteger c = *this;
		for (int i = 1; i <= c.s[0]; i++) {
			c.s[i] <<= a;
			if (c.s[i - 1] >= MOD) {
				c.s[i - 1] -= MOD;
				c.s[i]++;
			}
		}
		delzero;
		return c;
	}
	UnsignedInteger operator <<= (const long long& a) {
		return *this = *this << a;
	}
	
	UnsignedInteger operator >> (const long long& a) {
		UnsignedInteger c = *this;
		for (int i = c.s[0]; i >= 1; i--) {
			if ((c.s[i] & 1) && i > 1) c.s[i - 1] += MOD;
			c.s[i] >>= a;
		}
		delzero;
		return c;
	}
	UnsignedInteger operator >>= (const long long& a) {
		return *this = *this >> a;
	}
	
	UnsignedInteger operator / (const UnsignedInteger& k) {
		UnsignedInteger c = *this, a = k;
		UnsignedInteger tmp, lt;
		tmp.s[1] = 1;
		while (c >= a) {
			a <<= 1;
			tmp <<= 1;
		}
		while (tmp.s[0] > 1 || tmp.s[1]) {
			if (c >= a) {
				c -= a;
				lt += tmp;
			}
			a >>= 1;
			tmp >>= 1; 
		} 
		c = lt;
		delzero;
		if (c.s[0] < 1) c.s[c.s[0] = 1] = 0;
		return c;
 	}
	UnsignedInteger operator /= (const UnsignedInteger& a) {
 		return *this = *this / a;
	}
	
	UnsignedInteger operator % (const UnsignedInteger& a) {
		UnsignedInteger d = *this, c = *this / a;
		c *= a;
		return d - c;
	}
	UnsignedInteger operator %= (const UnsignedInteger& a) {
 		return *this = *this % a;
	}
	
	friend ostream& operator << (ostream& out, const UnsignedInteger &a) {
		out << a.s[a.s[0]];
		for (int i = a.s[0] - 1; i >= 1; i--) {
			out << setw(BIT) << setfill('0') << a.s[i];
		}
		return out;
	}
	
	friend istream& operator >> (istream& in, UnsignedInteger& a) {
		char s[N];
		in >> s;
		a = s;
		return in;
	}
};

class SignedInteger {
private:
	SignedInteger init() {
		flag = true;
		n = 0;
		return *this;
	}

	SignedInteger init(const long long num) {
		char s[N];
		sprintf(s, "%lld", num);
		return *this = s;
	}
	
	SignedInteger init(const char* num) {
		flag = num[0] != '-';
		n = num[0] != '-' ? num : num + 1;
		return *this;
	}
	
	SignedInteger init(const UnsignedInteger& num) {
		flag = true;
		n = num;
		return *this;
	}
	
public:
	UnsignedInteger n;
	bool flag;
	
	SignedInteger() {init();}
	SignedInteger(const int num) {init((long long) num);}
	SignedInteger(const long long num) {init(num);}
	SignedInteger(const char* num) {init(num);}
	SignedInteger(const UnsignedInteger& num) {init(num);}
	
	SignedInteger operator = (const int num) {return init((long long) num);}
	SignedInteger operator = (const long long num) {return init(num);}	
	SignedInteger operator = (const char* num) {return init(num);}
	SignedInteger operator = (const UnsignedInteger& num) {return init(num);}
	
	SignedInteger operator + (SignedInteger& a) {
		SignedInteger c;
		if (flag && a.flag) {
			c.flag = true;
			c.n = n + a.n;
		} else if (flag && !a.flag) {
			c = *this - a;
		} else if (!flag && a.flag) {
			c = a - *this;
		} else {
			c.flag = false;
			c.n = n + a.n;
		}
		return c;
	}
	SignedInteger operator += (SignedInteger &a) {
		return *this = *this + a;
	}
	
	SignedInteger operator - (SignedInteger& a) {
		SignedInteger c;
		if (n >= a.n) {
			c.flag = true;
			c.n = n - a.n;
		} else {
			c.flag = false;
			c.n = a.n - n;
		}
		return c;
	}
	SignedInteger operator -= (SignedInteger &a) {
		return *this = *this - a;
	}
	
	SignedInteger operator * (SignedInteger& a) {
		SignedInteger c;
		c.flag = flag == a.flag;
		c.n = n * a.n;
		return c;
	}
	SignedInteger operator *= (SignedInteger &a) {
		return *this = *this * a;
	}
	
	SignedInteger operator / (SignedInteger& a) {
		SignedInteger c;
		c.flag = flag == a.flag;
		c.n = n / a.n;
		return c;
	}
	SignedInteger operator /= (SignedInteger &a) {
		return *this = *this / a;
	}
	
	SignedInteger operator % (SignedInteger& a) {
		SignedInteger d = *this, c = *this / a;
		c *= a;
		return d - c;
	}
	SignedInteger operator %= (SignedInteger& a) {
 		return *this = *this % a;
	}
	
	friend ostream& operator << (ostream& out, const SignedInteger &a) {
		if (!a.flag) out << '-';
		return out << a.n;
	}
	
	friend istream& operator >> (istream& in, SignedInteger& a) {
		char s[N];
		in >> s;
		a = s;
		return in;
	}
};

#endif   // BIG_INTEGER_H

SignedInteger a, b;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	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;
}

2023/7/18 11:42
加载中...