#P8788. Xiaolan's Key and Permutation Distance

    ID: 21952 Type: Default 1000ms 256MiB

Xiaolan's Key and Permutation Distance

Xiaolan's Key and Permutation Distance

This contest consists of two result‐fill problems. You are required to output a single integer answer depending on the problem selected.

Problem A: Xiaolan and the Keys

Xiaolan is a kindergarten teacher. In his class there are 28 children. Each of the 28 children has a room with a unique key that opens only that room. In a game, all 28 keys are collected and then randomly redistributed among the children. There are \(28!\) possible assignments. Xiaolan is interested in the number of assignments in which exactly 14 children receive the key to their own room and the other 14 do not. It can be shown that the answer is \[ \binom{28}{14}\,D_{14}, \] where \(D_n\) denotes the number of derangements of \(n\) items (i.e. permutations with no fixed points), which satisfies \[ D_n = n!\sum_{i=0}^{n} \frac{(-1)^i}{i!}. \] This is a result‐fill problem: compute the integer and output it.

Problem B: Permutation Distance

Xiaolan is fascinated by permutations. He considers all permutations of a 17-character string made up of the letters abcdefghijklnopqr (i.e. the letters from a to r except m). In any permutation, each letter appears exactly once. The lexicographic order of these permutations is cyclic, meaning that after the last permutation the sequence returns to the first.

Two operations are permitted:

  1. Move to the next permutation (if at the last, wrap around to the first).
  2. Move to the previous permutation (if at the first, wrap around to the last).

Given two fixed permutations:

  • Permutation A: aejcldbhpiogfqnkr
  • Permutation B: ncfjboqiealhkrpgd
Determine the minimum number of moves (using only the two permitted operations) required to transform permutation A into permutation B. Again, output the integer answer.

Input: A single character ('A' or 'B') indicating the problem to solve.

Note: For Problem A, compute \(\binom{28}{14}\,D_{14}\) and for Problem B, compute the minimal cyclic distance between the lexicographic positions of the given fixed permutations.

inputFormat

A single line containing a single character: either 'A' or 'B'.

outputFormat

A single integer which is the answer to the selected problem.

sample

A
1283376422237413400