高精压位(已封装)

自己写的高精压位板子(限于正整数之间的四则预算)

多次使用交题未出现问题,可安心食用(雾),除法不太会,我写的容易TLE和MLE,需要高精除高精,高精模高精不建议使用此模板,不涉及高精除高精和高精模高精时可去掉高精除高精,高精模高精操作及辅助数组pw[]

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<cstdio>

#define int long long
#define iter vector<int>::iterator

using namespace std;

const int base = 100000000;
const int N = 300;

int cnt;//2的最高次方

struct BigInteger {

	vector<int> ans;
	bool sign;

	BigInteger();
	BigInteger(int);

	bool operator ==(const BigInteger&)const;
	bool operator <(const BigInteger&)const;
	bool operator >(const BigInteger&)const;
	bool operator <=(const BigInteger&)const;
	bool operator >=(const BigInteger&)const;
	bool operator !=(const BigInteger&)const;

	BigInteger operator = (const BigInteger&);
	BigInteger operator - (const BigInteger&) const;
	BigInteger operator + (const BigInteger&) const;
	BigInteger operator * (const BigInteger&) const;
	BigInteger operator * (const int&) const;
	BigInteger operator / (const BigInteger&) const;
	BigInteger operator / (const int&) const;
	BigInteger operator %(const BigInteger&)const;
	int operator % (const int&) const;
	BigInteger operator ^ (const int&) const;

	void operator -=(const BigInteger&);
	void operator +=(const BigInteger&);
	void operator *=(const BigInteger&);
	void operator /=(const BigInteger&);
	void operator %=(const BigInteger&);

	void input(string);
	void output();

}pw[N];

void div_ans_mod(BigInteger& x) {
	BigInteger p(2);
	pw[0] = BigInteger(1);
	pw[1] = p;
	while (pw[cnt] <= x) {
		++cnt;
		pw[cnt] = p * pw[cnt - 1];
	}
}

BigInteger::BigInteger() {
	sign = true;
	ans.clear();
}

BigInteger::BigInteger(int n) {
	sign = true;
	while (n) {
		ans.push_back(n % base);
		n /= base;
	}
}

void BigInteger::input(string str) {
	int top = str.length() - 1;
	while (top - 7 >= 0) {
		int temp = (str[top] - '0') + (str[top - 1] - '0') * 10 + (str[top - 2] - '0') * 100 + (str[top - 3] - '0') * 1000
			+ (str[top - 4] - '0') * 10000 + (str[top - 5] - '0') * 100000 + (str[top - 6] - '0') * 1000000 + (str[top - 7] - '0') * 10000000;
		top -= 8;
		ans.push_back(temp);
	}
	if (top >= 0) {
		int temp = 0;
		for (int i = 0; i <= top; i++) {
			temp = temp * 10 + (str[i] - '0');
		}
		ans.push_back(temp);
	}
}

void BigInteger::output() {
	if (sign == false) cout << "-";
	for (int i = ans.size() - 1; i > 0; i--) {
		if (ans[i] == 0) ans.pop_back();
		else break;
	}
	printf("%lld", ans.back());
	ans.pop_back();
	while (!ans.empty()) {
		printf("%08d", ans.back());
		ans.pop_back();
	}
	printf("\n");
}

bool BigInteger:: operator==(const BigInteger& x) const {
	if (ans.size() != x.ans.size()) return false;
	for (int i = ans.size() - 1; i >= 0; i--) {
		int a = ans[i];
		int b = x.ans[i];
		if (a != b) return false;
	}
	return true;
}

bool BigInteger::operator!=(const BigInteger& x)const {
	return !(x == *this);
}

bool BigInteger:: operator<(const BigInteger& x) const {
	if (ans.size() != x.ans.size()) return ans.size() < x.ans.size();
	for (int i = ans.size() - 1; i >= 0; i--) {
		int a = ans[i];
		int b = x.ans[i];
		if (a >= b) return false;
		if (a < b) return true;
	}
	return false;
}

bool BigInteger:: operator>(const BigInteger& x) const {
	if (ans.size() != x.ans.size()) return ans.size() > x.ans.size();
	for (int i = ans.size() - 1; i >= 0; i--) {
		int a = ans[i];
		int b = x.ans[i];
		if (a > b) return true;
		if (a <= b) return false;
	}
	return false;
}

bool BigInteger:: operator<=(const BigInteger& x) const {
	if (ans.size() != x.ans.size()) return ans.size() < x.ans.size();
	for (int i = ans.size() - 1; i >= 0; i--) {
		int a = ans[i];
		int b = x.ans[i];
		if (a > b) return false;
		if (a < b) return true;
	}
	return true;
}

bool BigInteger:: operator>=(const BigInteger& x) const {
	if (ans.size() != x.ans.size()) return ans.size() < x.ans.size();
	for (int i = ans.size() - 1; i >= 0; i--) {
		int a = ans[i];
		int b = x.ans[i];
		if (a > b) return true;
		if (a < b) return false;
	}
	return true;
}

BigInteger BigInteger :: operator +(const BigInteger& x) const {
	int mod = 0;
	BigInteger ret;
	int len = max(ans.size(), x.ans.size());
	for (int i = 0; i < len; i++) {
		mod += (i < ans.size()) ? ans[i] : 0;
		mod += (i < x.ans.size()) ? x.ans[i] : 0;
		ret.ans.push_back(mod % base);
		mod /= base;
	}
	if (mod) ret.ans.push_back(mod);
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator-(const BigInteger& x)const {
	BigInteger ret;
	bool flag = true;
	if (*this < x)flag = false;
	if (*this == x) {
		ret.ans.push_back(0);
		return ret;
	}
	else {
		int len = max(ans.size(), x.ans.size());
		int mod = 0;
		for (int l = 0; l < len; l++) {
			if (flag) {
				mod += (l < ans.size()) ? ans[l] : 0;
				mod -= (l < x.ans.size()) ? x.ans[l] : 0;
			}
			else {
				mod += (l < x.ans.size()) ? x.ans[l] : 0;
				mod -= (l < ans.size()) ? ans[l] : 0;
			}
			ret.ans.push_back((mod + base) % base);
			if (mod < 0) mod = -1;
			else mod = 0;
		}
		ret.sign = flag;
	}
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator=(const BigInteger& x) {
	ans.clear();
	sign = x.sign;
	for (int i = 0; i < x.ans.size(); i++) {
		ans.push_back(x.ans[i]);
	}
	return *this;
}

BigInteger BigInteger:: operator*(const int& x) const {
	BigInteger ret;
	int len = ans.size();
	for (int i = 0; i <= len; i++) {
		ret.ans.push_back(0);
	}
	int mod = 0;
	for (int i = 0; i < ans.size(); i++) {
		mod += ans[i] * x;
		if (mod >= base) {
			ret.ans[i] = mod % base;
			ret.ans[i + 1] = mod / base;
		}
		else {
			ret.ans[i] = mod;
		}
		mod = 0;
	}
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator*(const BigInteger& x)const {
	BigInteger ret;
	int len1 = ans.size();
	int len2 = x.ans.size();
	int len = len1 + len2;
	for (int i = 0; i <= len; i++) {
		ret.ans.push_back(0);
	}
	int mod = 0;
	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < x.ans.size(); j++) {
			mod += x.ans[j] * ans[i];
			mod += ret.ans[i + j];
			if (mod >= base) {
				ret.ans[i + j] = mod % base;
				ret.ans[i + j + 1] += mod / base;
			}
			else {
				ret.ans[i + j] = mod;
			}
			mod = 0;
		}
	}
	if (sign != x.sign) ret.sign = false;
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator/(const int& x) const {
	BigInteger ret;
	int down = 0;
	int p = 0;
	for (int i = ans.size() - 1; i >= 0; i--) {
		down = ans[i] + down * base;
		if (down == 0 && i) {
			ret.ans.insert(ret.ans.begin(), 0);
		}
		else {
			int in = down / x;
			ret.ans.insert(ret.ans.begin(), in);
			down %= x;
		}
	}
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator /(const BigInteger& x) const {
	BigInteger ret;
	int nod = 0;
	BigInteger temp = *this;
	for (int i = cnt; i >= 0; i--) {
		if (pw[i] * x <= temp) {
			BigInteger t = pw[i] * x;
			ret += pw[i];
			temp -= t;
		}
	}
	if (sign != x.sign) ret.sign = false;
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

BigInteger BigInteger::operator%(const BigInteger& x) const {
	BigInteger ret;
	BigInteger temp = *this;
	for (int i = cnt; i >= 0; i--) {
		if (pw[i] * x <= temp) {
			temp -= pw[i] * x;
		}
	}
	ret = temp;
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

int BigInteger:: operator%(const int& x)const {
	BigInteger ret;
	int down = 0;
	for (int i = ans.size() - 1; i >= 0; i--) {
		down = ans[i] + down * base;
		down %= x;
	}
	return down;
}

BigInteger BigInteger::operator^(const int& n) const {
	BigInteger ret(1);
	BigInteger temp = *this;
	int pow = n;
	while (pow) {
		if (pow & 1) ret = (ret * temp);
		temp = temp * temp;
		pow >>= 1;
	}
	while (ret.ans.size() > 1 && ret.ans.back() == 0)ret.ans.pop_back();
	if (ret.ans.empty())ret.ans.push_back(0);
	return ret;
}

void BigInteger::operator+=(const BigInteger& x) {
	*this = *this + x;
}

void BigInteger::operator-=(const BigInteger& x) {
	*this = *this - x;
}

void BigInteger::operator*=(const BigInteger& x) {
	*this = *this * x;
}

void BigInteger::operator/=(const BigInteger& x) {
	*this = *this / x;
}

void BigInteger::operator%=(const BigInteger& x) {
	*this = *this % x;
}

signed main() {
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值