As the title says, I am looking for a way to count permutations of $n$ (permutations of the set $(0, 1,\dots, n-1)$) where $k, 0 \le k \le n$ items should not stay in same place (be deranged), while rest $n-k$ items may be placed anywhere (even stay in same place).

This is needed in an algorithm of mine to rank derangements in lexicographic order.

Initially I thought that $\text{count} = (n-k)! \cdot !k$, that is number of derangements of $k$ items multiplied by any permutation of the rest, but I am not so sure.

**NOTE** $k$ is known and fixed beforehand, we do not choose which $k$ out of $n$ should be deranged, but may not necessarily be the first $k$ items, may be any $k$ items out of $n$, but fixed.

Alternatively if an algorithm is known to rank derangements in lexicographic order (**not** in cycle notation), I am happy.