The idea of this question is similar to that of this question.
If f [I] [J] [0/1] [0/1] is set to the I-th digit, at least J locations are allowed.
And the number I has/has not been used, and the number I + 1 has/has not been used
Choose whether this location meets the conditions for each transfer.
If he meets the conditions, he will be transferred from the status that has not been filled with I-1.
Fill in an empty number if the condition is not met.
As long as the factorial of the total number of null numbers is multiplied at the end, it is the desired number of sorted numbers.
However, in this way, there may be more matching positions in a certain arrangement.
But at least J are guaranteed.
In this way, we can finally calculate at least J solutions that meet the conditions.
Just try again.
That is, ANS = at least k-C (k + 1, k) * at least (k + 1) + C (K + 2, K) * at least (K + 2) -...
Just calculate n, because the total length is N.
Code:
# Include <algorithm> # include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <cctype> using namespace STD; typedef long ll; const int max_n = 1005; const ll mod = limit 7ll; int N, K; int PI [max_n], C [max_n] [max_n]; int f [max_n] [max_n] [2] [2], at_least [max_n]; inline void add (Int & X, int y) {x = (x + y) % MOD; return;} int main () {scanf ("% d", & N, & K); Pi [0] = 1; for (INT I = 1; I <= N; ++ I) PI [I] = int (1ll * PI [I-1] * I % mod ); f [0] [0] [1] [0] = 1; for (INT I = 1; I <= N; ++ I) {register int maxj = min (I, n); For (Int J = 0; j <= maxj; ++ J) {Add (F [I] [J] [0] [0], INT (1ll * (F [I-1] [J] [0] [0] + F [I-1] [J] [1] [0]) % mod); add (F [I] [J] [1] [0], (f [I-1] [J] [0] [1] + F [I-1] [J] [1] [1]) % mod ); add (F [I] [J + 1] [0] [0], F [I-1] [J] [0] [0]); add (F [I] [J + 1] [1] [0], F [I-1] [J] [0] [1]); add (F [I] [J + 1] [0] [1], INT (1ll * (F [I-1] [J] [0] [0] + F [I-1] [J] [1] [0]) % mod); add (F [I] [J + 1] [1] [1], INT (1ll * (F [I-1] [J] [0] [1] + F [I-1] [J] [1] [1]) % mod) ;}}for (INT I = 0; I <= N; ++ I) at_least [I] = 1ll * (F [N] [I] [0] [0] + F [N] [I] [1] [0]) % mod * PI [n-I] % MOD; For (INT I = 0; I <= N; ++ I) {c [I] [0] = C [I] [I] = 1; for (Int J = 1; j <I; ++ J) c [I] [J] = int (1ll * (C [I-1] [J-1] + C [I-1] [J]) % mod);} int ans = at_least [k]; for (INT I = k + 1; I <= N; ++ I) {ans = (1ll * (ANS + (I-k) & 1 )? -1ll: 1ll) * C [I] [k] * at_least [I]) % mod + mod) % MOD;} printf ("% d \ n", ANS ); return 0 ;}
Cf285e positions in Permutations