#P4609. Skyscraper Permutations

    ID: 17855 Type: Default 1000ms 256MiB

Skyscraper Permutations

Skyscraper Permutations

Famous architect Little Z has been assigned a peculiar task: to construct n skyscrapers on a number line, each with a unique height from 1 to n (no two buildings have the same height). Little Z further believes that the skyline has a unique beauty when exactly A skyscrapers are visible from the left and exactly B skyscrapers are visible from the right.

A skyscraper is visible from the left (or right) if there is no building to its left (or right) that is taller. Formally, if we denote the number of visible skyscrapers when viewed from the left as A and from the right as B, then the number of valid arrangements is the number of permutations of {1, 2, ..., n} which have exactly A left-to-right maxima and B right-to-left maxima.

The recurrence relation for the number of configurations, \( f(n, A, B) \), is given by:

\( f(n, A, B) = f(n-1, A-1, B) + f(n-1, A, B-1) + (n-2) \times f(n-1, A, B) \)

with the base case \( f(1, 1, 1) = 1 \) and \( f(n, A, B) = 0 \) if either A or B is out of the valid range.

inputFormat

The input consists of a single line containing three integers: n (the number of skyscrapers), A (the number of skyscrapers visible from the left), and B (the number visible from the right).

outputFormat

Output a single integer representing the number of distinct skyscraper arrangements that satisfy the conditions.

sample

4 2 2
2