Skip to content

Latest commit

 

History

History
28 lines (15 loc) · 968 Bytes

abc273_a_a_recursive_function.md

File metadata and controls

28 lines (15 loc) · 968 Bytes

A Recursive Function

Original problem · C++ solution

Try first

The recurrence multiplies all positive integers through N, starting with the base value one.

Reasoning

The recurrence multiplies all positive integers through N, starting with the base value one. An empty product correctly handles N=0.

Cost

  • Time: O(n).
  • Extra space: O(1).

C++ takeaway

The types of operands control intermediate arithmetic. Read large values into long long before forming sums, products, or differences so the arithmetic itself uses 64 bits.

Watch for

Integer division truncates toward zero; floor and ceiling formulas need special care for negative values.

Explain it back

State the invariant without looking at the code. Give one input that breaks the most tempting incorrect approach, then add it to the tests.