Hello codeforces community!!
On this occasion I bring up a combinatorics problem.
This is the problem:
How many permutations of length N exist such that the following holds: for each i (1 <= i < N) p[i+1] — p[i] != 1
Constants: 1 <= N <= 500
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 155 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 134 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
Hello codeforces community!!
On this occasion I bring up a combinatorics problem.
This is the problem:
How many permutations of length N exist such that the following holds: for each i (1 <= i < N) p[i+1] — p[i] != 1
Constants: 1 <= N <= 500
| Name |
|---|



Auto comment: topic has been updated by Kanata18 (previous revision, new revision, compare).
I haven't proved my solution yet, but this should solve the task in $$$\mathcal{O}(n)$$$.
Basically, I though about decomposing the permutation into $$$k$$$ subarrays that each subarray satisfies the condition that the next term is +1 the last term. If this is possible, then this permutation is $$$k$$$-good We can look at this decomposition as also a permutation. We want the number of permutations that is $$$n$$$-good but not $$$n-1$$$-good. This makes me want to try out some inclusion-exclusion, similar to counting derangements. The exact expression is done by changing the formula bit-by-bit and compare with brute-force results.
It occurred to me to do it using: Connected Component DP (although I don't know how to implement it)
This problem is the exact same as CSES — Permutations II, although the constraints are $$$n \leq 5000$$$.
Note, this task could be solved in $$$O(n)$$$, you can check it on USACO Guide — Solution
Thank you very much for the material