程序模板——高精度及相关基础知识
flame_fire · · 算法·理论
高精度完整知识点
一、高精度适用场景C++ 内置整型(int/long long)有上限:
int:-2^31 ~ 2^31-1(约 2e9)
long long:-2^63 ~ 2^63-1(约 9e18)
当数字超过上限(几百 / 几千位大整数),必须用数组 或vector 模拟手工竖式运算,即高精度(大整数)。
二、高精度核心基础概念
1. 存储方式(两种主流)
(1)低位存在数组下标 0(推荐,运算方便)
例:数字
1234 存为[4,3,2,1]
优势:进位 / 借位直接向后(下标增大),不用频繁移位。
2)高位存在数组下标 0(仅输入输出直观,运算麻烦)
例:
1234 → [1,2,3,4] ,不推荐做运算模板。2. 存储容器选择
- vector<int>:动态长度,自动扩容,最通用(模板主力)
- int a[10005]:定长数组,速度略快,需预估最大位数
- 压位优化:不存 1 位数字,一次存 4/9 位
(1e4/1e9) ,减少循环次数,提速数倍3. 基础预处理函数
- 字符串转高精度数组:读入字符串,逆序存入 vector(低位在前)
- 高精度数组转字符串输出:逆序遍历输出,去除前导零
- 比较函数:高精度大小比较(用于减法、除法判正负)
三、高精度四则运算原理(手工竖式模拟)
1. 高精度加法(无负数)
逐位相加,带进位:
当前位:
循环到两个数全部遍历且进位为 0 结束。
2. 高精度减法(保证 a ≥ b,无负数)
逐位相减,借位:
若
否则
最后删除末尾前导零(vector 尾部 0)。
3. 高精度乘法(高精 × 高精)双重循环:
4. 高精度除法(高精 ÷ 低精,高精 ÷ 高精拓展)
- 高精 ÷ 普通整数(long long):从高位向低位逐位求商,记录余数
- 高精 ÷ 高精:二分试商法,用减法模拟试商,复杂度更高
四、通用基础模板
头文件 + 全局定义
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std;
using BigInt = vector<int>;
// 字符串转大整数(低位在前) BigInt str2big(string s) { BigInt res; reverse(s.begin(), s.end()); for (char c : s) res.push_back(c - '0'); return res; }
// 大整数转字符串输出 string big2str(const BigInt& a) { string s; for (auto it = a.rbegin(); it != a.rend(); ++it) s += to_string(*it); if (s.empty()) s = "0"; return s; }
// 比较 a,b:a>b返回1;a==b返回0;a<b返回-1 int cmp(const BigInt& a, const BigInt& b) { if (a.size() != b.size()) return a.size() > b.size() ? 1 : -1; for (int i = a.size()-1; i >= 0; --i) { if (a[i] != b[i]) return a[i] > b[i] ? 1 : -1; } return 0; }
#### 1. 高精度加法
```cpp
BigInt add(const BigInt& a, const BigInt& b) {
BigInt res;
int carry = 0;
int maxlen = max(a.size(), b.size());
for (int i = 0; i < maxlen || carry; ++i) {
if (i < a.size()) carry += a[i];
if (i < b.size()) carry += b[i];
res.push_back(carry % 10);
carry /= 10;
}
return res;
}
2. 高精度减法(a >= b)
BigInt sub(const BigInt& a, const BigInt& b) {
BigInt res;
int borrow = 0;
for (int i = 0; i < a.size(); ++i) {
int cur = a[i] - borrow;
if (i < b.size()) cur -= b[i];
borrow = 0;
if (cur < 0) {
cur += 10;
borrow = 1;
}
res.push_back(cur);
}
// 去除前导零(尾部0)
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
3. 高精度 × 高精度
BigInt mul(const BigInt& a, const BigInt& b) {
BigInt res(a.size() + b.size(), 0);
for (int i = 0; i < a.size(); ++i) {
int carry = 0;
for (int j = 0; j < b.size() || carry; ++j) {
if (j < b.size()) carry += a[i] * b[j];
carry += res[i + j];
res[i + j] = carry % 10;
carry /= 10;
}
}
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
4. 高精度 ÷ 低精(long long 除数),返回商,余数传参
BigInt div_low(const BigInt& a, long long b, long long& rem) {
BigInt res;
rem = 0;
// 从高位到低位计算
for (int i = a.size() - 1; i >= 0; --i) {
rem = rem * 10 + a[i];
res.push_back(rem / b);
rem %= b;
}
reverse(res.begin(), res.end());
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
5. 主函数测试示例
BigInt div_low(const BigInt& a, long long b, long long& rem) {
BigInt res;
rem = 0;
// 从高位到低位计算
for (int i = a.size() - 1; i >= 0; --i) {
rem = rem * 10 + a[i];
res.push_back(rem / b);
rem %= b;
}
reverse(res.begin(), res.end());
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
拓展 1:压位高精度
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
using BigInt = vector<long long>;
const long long BASE = 10000;
const int WIDTH = 4;
// 字符串转压位大整数
BigInt str2big(string s) {
BigInt res;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i += WIDTH) {
long long val = 0;
long long pow10 = 1;
for (int j = 0; j < WIDTH && i + j < s.size(); ++j) {
val += (s[i + j] - '0') * pow10;
pow10 *= 10;
}
res.push_back(val);
}
return res;
}
// 压位大数输出
void print(const BigInt& a) {
printf("%lld", a.back());
for (int i = a.size() - 2; i >= 0; --i) {
printf("%04lld", a[i]);
}
puts("");
}
// 压位加法
BigInt add(const BigInt& a, const BigInt& b) {
BigInt res;
long long carry = 0;
int maxlen = max(a.size(), b.size());
for (int i = 0; i < maxlen || carry; ++i) {
if (i < a.size()) carry += a[i];
if (i < b.size()) carry += b[i];
res.push_back(carry % BASE);
carry /= BASE;
}
return res;
}
// 压位乘法
BigInt mul(const BigInt& a, const BigInt& b) {
BigInt res(a.size() + b.size(), 0);
for (int i = 0; i < a.size(); ++i) {
long long carry = 0;
for (int j = 0; j < b.size() || carry; ++j) {
if (j < b.size()) carry += a[i] * b[j];
carry += res[i + j];
res[i + j] = carry % BASE;
carry /= BASE;
}
}
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
BigInt a = str2big(s1), b = str2big(s2);
BigInt c = add(a, b);
cout << "和:"; print(c);
BigInt d = mul(a, b);
cout << "积:"; print(d);
return 0;
}
拓展 2:高精 ÷ 高精
// 高精 × 低精
BigInt mul_low(const BigInt& a, long long k) {
BigInt res;
long long carry = 0;
for (long long num : a) {
carry += num * k;
res.push_back(carry % 10);
carry /= 10;
}
while (carry) {
res.push_back(carry % 10);
carry /= 10;
}
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
// 高精 ÷ 高精,返回商
BigInt div_big(BigInt a, BigInt b) {
BigInt l = str2big("0"), r = a, ans;
while (cmp(l, r) <= 0) {
BigInt mid = add(l, r);
long long rem;
mid = div_low(mid, 2, rem);
BigInt tmp = mul(mid, b);
if (cmp(tmp, a) <= 0) {
ans = mid;
l = add(mid, str2big("1"));
} else {
r = sub(mid, str2big("1"));
}
}
return ans;
}
拓展 3:高精度阶乘
BigInt factorial(int n) {
BigInt res = str2big("1");
for (int i = 2; i <= n; ++i) {
res = mul_low(res, i);
}
return res;
}
拓展 4:带符号高精
struct Num {
BigInt val;
int sign; // 1正数 -1负数
Num() : sign(1) {}
Num(string s) {
sign = 1;
if (s[0] == '-') {
sign = -1;
val = str2big(s.substr(1));
} else {
val = str2big(s);
}
}
};
// 重载运算符、统一正负运算逻辑,可自行拓展operator+ operator-
五、常见易错点总结
- 前导零:减法、乘法后必须删除 vector 末尾 0,否则输出多余 0
- 进位 / 借位循环边界:加法循环条件 i < maxlen || carry,防止最后进位丢失
- 减法前提:未判断大小直接相减会出现负数借位错误
- 压位输出补零:中间段不足 4 位要补 0,否则数字错位
- 除法高位遍历:高精除低精必须从数组尾部(高位)向前遍历
- __int128 限制:仅能内部运算,不能 cin/cout/printf 输出
想了解更多,可加入南瓜小队(点我) 哦 !