Ranging from the number of columns down the valley P2401

Topic Link .

Solution

State design

Set \ (f_ {i, j} \) of \ (1 \) to \ (I \) are arranged, which \ (J \) a \ (\ text { '<' } \) of the number of programs.

State transition

Try from (I \) \ proceeds to \ (i + 1 \) , the substance is considered to \ (i + 1 \) is inserted to which position of the sequence.

  • If you insert to the left, will add a greater-than sign ( \ (1 \) case)
  • If the insert to the right most, will add less than a number ( \ (1 \) case)
  • If you inserted between a less-than, less-than destroy a generate a number less than and greater than, greater than the equivalent of a new number ( \ (J \) case)
  • If the number is greater than one between the insert, destroy a number greater than, produce a greater-than and less than a number, less than the equivalent of a new number ( \ (I -. 1 - J \) case)

In summary:

  • There \ (j + 1 \) case is greater than the increase in a number, i.e. \ (f [i + 1] [j] \ gets f [i] [j] * (j + 1) \)
  • There \ (i - j \) case increasing a number less than that \ (f [i + 1] [j + 1] \ gets f [i] [j] * (i - j) \)

Code

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 1005, P = 2015;

int n, K, f[N][N];

int main() {
    scanf("%d%d", &n, &K);
    f[1][0] = 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            (f[i + 1][j] += f[i][j] * (j + 1)) %= P;
            (f[i + 1][j + 1] += f[i][j] * (i - j)) %= P;
        }
    }
    printf("%d\n", f[n][K]);
}

Guess you like

Origin www.cnblogs.com/dmoransky/p/12482188.html