跳到主要内容

T.11 · 弱组合、循环不变量与流程图追踪

GRE 数学 · 知识点 26

本主题的视频课程 打开视频页面
4:51

T.11 · 弱组合、循环不变量与流程图追踪

本单元统计带非负或正约束的不可区分分配,按语句顺序追踪流程图并进行正确复位,并运用…

英文讲解 · 可切换中英文字幕

26.1

弱组合、循环不变量与流程图追踪

English

In 1969 Tony Hoare, a British computer scientist, published "An Axiomatic Basis for Computer Programming", building on work by Robert Floyd. Its central idea was simple: to prove a loop correct, find a statement that is true before the loop starts and stays true after every pass. When the loop stops, that statement, together with the stopping condition, gives the answer. This loop invariant 循环不变量 turns tracing a program into a short proof. Hoare later received the Turing Award.

GRE Mathematics counting and algorithm questions ask you to:

  • Count each weak composition 弱组合分拆 with stars and bars.
  • Handle lower bounds by shifting and upper bounds by inclusion–exclusion.
  • Trace loops and flowcharts, counting iterations.
  • State and use a loop invariant.
词汇 训练
English 中文 拼音
loop invariant/luːp ɪnˈveərɪənt/ 循环不变量 xún huán bù biàn liàng
weak composition/wiːk ˌkɒmpəˈzɪʃn/ 弱组合分拆 ruò zǔ hé fēn chāi
26.2

隔板法

English

The number of solutions of $x_1 + \cdots + x_k = n$ in nonnegative integers is

$$\binom{n + k - 1}{k - 1}$$
  • Picture $n$ stars and $k - 1$ bars in a row; the bars split the stars into $k$ groups.
  • Positive integers (each $x_i \ge 1$): $\dbinom{n - 1}{k - 1}$.
  • Lower bounds: substitute $x_i = u_i + m_i$ to remove each bound $x_i \ge m_i$.
  • Upper bounds: count all solutions, then subtract those that break a bound, with inclusion–exclusion.
Two stars, a bar, three stars, a bar: 5 identical items in 3 boxes as 2, 3, 0; choosing 2 bar places in 7 slots gives 21 solutions
Stars and bars: C(n + k - 1, k - 1).
26.3

跟踪算法

English
  • Make a table with one column per variable and one row per pass, and update the variables in the order the code does.
  • Check the loop condition at the right moment: before the body for "while", after it for "repeat until".
  • Count iterations carefully: off-by-one errors are the commonest mistake.
26.4

循环不变量

English

A loop invariant is a statement that is:

  • True before the first iteration.
  • Kept true by every iteration.
  • Useful at the end: combined with the stopping condition, it gives the result.
Table for an odd-sum loop: after m iterations the sum S is m² and the next addend B is 2m + 1
The loop invariant S = m² holds before each addition
26.5

例题:有界解

English

Count nonnegative solutions of $a + b + c = 8$ with $a \ge 2$.

  • Put $u = a - 2$: $u + b + c = 6$ has $\binom82 = 28$ solutions.
  • With also $b, c \ge 1$: subtract 1 from each to get a total of 4, so $\binom62 = 15$.
  • So $28 - 15 = 13$ have $b = 0$ or $c = 0$. Check: $b = 0$ gives 7, $c = 0$ gives 7, both zero is counted twice: $7 + 7 - 1 = 13$.
26.6

例题:奇数循环

English
S = 0; B = 1
while B <= 2n - 1:
    S = S + B
    B = B + 2
output S
  • Invariant after $m$ passes: $S = m^2$ and $B = 2m + 1$. It holds at $m = 0$, and one pass gives $S = m^2 + 2m + 1 = (m + 1)^2$, $B = 2(m + 1) + 1$.
  • The loop stops when $B = 2n + 1$, that is after $n$ passes, so the output is $n^2$. For $n = 4$: 16.
  • The loop while x > 1: x = x // 2, from $x = 100$, runs through 50, 25, 12, 6, 3, 1: six iterations.
26.7

常见错误

English
  • Using $\binom{n + k - 1}{k}$ with the wrong lower index, or mixing up nonnegative and positive solutions.
  • Forgetting to shift lower bounds before counting.
  • Counting a loop condition check as an extra pass.
  • Stating an invariant that is true at the end but not kept by each pass.
  • Updating two variables in the wrong order when tracing.
26.8

关键术语

English

weak composition: An ordered way of writing a number as a sum of nonnegative integers.

loop invariant: A statement that is true before and after every iteration of a loop.

26.9

自我检测

English
  • How many nonnegative solutions does $x + y + z = 10$ have?
  • How many positive solutions does $a + b + c + d = 10$ have?
  • What does the odd-number loop output for $n = 7$?

Answers: $\binom{12}{2} = 66$. $\binom93 = 84$. 49.

该知识点的互动课程

逐步完成,配合即时检查练习。

更多 GRE 数学 知识点

登录或创建账户

IGCSE, A-Level & AP