> 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/algorithms/enumeration-simulation.md).

# 枚举法与模拟法

### 枚举法

枚举法把所有可能的情况一一列举出来，逐一检验，找出满足条件的解。

特点：**思路简单、正确性高**，但可能耗时，需要估算规模。

```cpp
// 求 1~100 中所有完数（等于除自身外所有约数之和）
for (int n = 2; n <= 100; n++) {
    int sum = 0;
    for (int i = 1; i < n; i++)
        if (n % i == 0) sum += i;
    if (sum == n) cout << n << " ";  // 6 28
}
```

### 模拟法

模拟法按照题目描述的规则，**一步步重现过程**，不作数学抽象。

```cpp
// 模拟：约瑟夫环（n 个人报数到 m 出列）
int n = 5, m = 3, cnt = 0, out = 0;
bool vis[10] = {false};
for (int i = 0; out < n; i = (i + 1) % n) {
    if (vis[i]) continue;
    if (++cnt == m) {
        vis[i] = true; out++; cnt = 0;
        cout << i + 1 << " ";
    }
}
```

### 使用要点

* 先估算枚举规模，避免超时（如双重循环到 10⁵ 就可能超时）。
* 模拟题关键是**把规则拆成清晰的步骤**，注意边界与循环结束条件。


---

# 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/algorithms/enumeration-simulation.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.
