> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/number-theory/big-number.md).

# 高精度运算

### 为什么需要高精度

C++ 的 `long long` 最大约 9.2×10¹⁸，超过就溢出。当数值上千位时，用**数组模拟**大整数运算，即高精度。

### 存储方式

把数字的每一位存进数组，通常**倒序存放**（个位在 a\[0]），便于进位。

```cpp
string s = "12345";
int a[100] = {0}, len = s.size();
for (int i = 0; i < len; i++)
    a[i] = s[len - 1 - i] - '0';  // 逆序存入：a[0]=5, a[1]=4...
```

### 高精度加法

```cpp
int c[105] = {0};
for (int i = 0; i < max(len1, len2); i++) {
    c[i] += a[i] + b[i];
    c[i + 1] += c[i] / 10;   // 进位
    c[i] %= 10;
}
```

### 高精度减法

处理借位，并去除前导零，注意比较大小确定符号。

### 高精度乘法

```cpp
for (int i = 0; i < len1; i++)
    for (int j = 0; j < len2; j++) {
        c[i + j] += a[i] * b[j];
        c[i + j + 1] += c[i + j] / 10;
        c[i + j] %= 10;
    }
```

### 高精度除法

从高位到低位试商，与手算除法一致（较复杂，可结合二分试商）。

> 考纲要求：用数组模拟高精度加、减、乘、除。乘法结果位数最多为 len1+len2。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/number-theory/big-number.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
