跳到正文

个人记录

2.1 总体复盘 / My 高精度板子.

Shaoxing_Sun约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年2月1日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

题目


T1 / T2 / T3 / T6

均为高精度板子,高精度则是可以进行摆竖式这样去模拟,并同时模拟进位与借位即可,在处理细节时有一定的难度。其中高精度乘高精度的运算时间复杂度为 O(n2)O(n^2)。

一般这类题目我直接使用封装好的大模板:

C++
struct BigInt {
	int a[10087];
	int len;
	BigInt(std::string s = "") {
		memset(a, 0, sizeof(a));
		len = s.size();
		for (int i = 0; i < len; i++) {
			a[i] = s[len - i - 1] - '0';
		}
	}
	BigInt(long long x) {
		char cc[10087];
		sprintf(cc, "%lld", x);
		std::string s = cc;
		*this = s;
	}
	BigInt(int x) {
		char cc[10087];
		sprintf(cc, "%d", x);
		std::string s = cc;
		*this = s;
	}
	friend const int comp(const BigInt &a, const BigInt &b) {
		if (a.len > b.len) {
			return 1;
		}
		if (a.len < b.len) {
			return -1;
		}
		for (int i = a.len - 1; i >= 0; i--) {
			if (a.a[i] > b.a[i])return 1;
			if (a.a[i] < b.a[i])return -1;
		}
		return 0;
	}
	friend bool operator<(const BigInt& a, const BigInt &b) {
		return comp(a, b) == -1;
	}
	friend bool operator>(const BigInt& a, const BigInt &b) {
		return comp(a, b) == 1;
	}
	friend  bool operator==(const BigInt& a, const BigInt &b) {
		return comp(a, b) == 0;
	}
	friend bool operator<=(const BigInt& a, const BigInt &b) {
		return comp(a, b) != 1;
	}
	friend bool operator>=(const BigInt& a, const BigInt &b) {
		return comp(a, b) != -1;
	}
   	 friend const BigInt operator+(const BigInt &a, const BigInt &b) {
		BigInt ans;
		ans.len = std::max(a.len, b.len);
		for (int i = 0; i < ans.len; i++) {
			ans.a[i] += a.a[i] + b.a[i];
			ans.a[i + 1] += ans.a[i] / 10;
			ans.a[i] %= 10;
		}
		if (ans.a[ans.len])ans.len++;
		return ans;
	}
	friend const BigInt operator-(BigInt a, BigInt b) {
		for (int i = 0; i < a.len; i++) {
			a.a[i] -= b.a[i];
			if (a.a[i] < 0) {
				a.a[i + 1]--;
				a.a[i] += 10;
			}
		}
		for (; a.a[a.len - 1] == 0 && a.len > 1; a.len--);
		return a;
	}
	friend const BigInt operator*(const BigInt &a, const BigInt &b) {
		BigInt ans;
		ans.len = a.len + b.len;
		for (int i = 0; i < a.len; i++) {
			for (int j = 0; j < b.len; j++) {
				ans.a[i + j] += a.a[i] * b.a[j];
			}
		}
		for (int i = 0; i < ans.len; i++) {
			ans.a[i + 1] += ans.a[i] / 10;
			ans.a[i] %= 10;
		}
		for (; ans.a[ans.len - 1] == 0 && ans.len > 1; ans.len--);
		return ans;
	}
	friend const BigInt operator/(const BigInt &a, const BigInt &b) {
		BigInt sum;
		BigInt ans = 0;
		ans.len = a.len;
		for(int i = a.len - 1; i >= 0; i--) {
			sum = sum * 10 + a.a[i];
			while(comp(sum, b) != -1) {
				sum = sum - b;
				ans.a[i]++;
			}
		}
		for (; ans.a[ans.len - 1] == 0 && ans.len > 1; ans.len--);
		return ans;
	}
	friend std::istream& operator>>(std::istream& is, BigInt &a) {
		std::string s;
		is >> s;
		a = s;
		return is;
	}
	friend std::ostream& operator<<(std::ostream& os, const BigInt &a) {
		for (int i = a.len - 1; i >= 0; i--)os << a.a[i];
		return os;
	}
};

T4

在高精度板子的基础上直接写就行了:

C++
BigInt res, sum = 1;
void solve() {
	int n;
	std::cin >> n;
	for (BigInt i = 1; i <= n; i = i + 1) {
		for (BigInt j = 1; j <= i; j = j + 1) {
			sum = sum * j;
		}
		res = res + sum;
		sum = 1;
	}
	std::cout << res << std::endl;
}

T5

熟悉的题目。 先把输赢情况给打表打出来,然后进行周期性循环即可。


估分 600600 PTS,实际 600600 PTS。

原文