Cf285e positions in Permutations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.