luogu P2401 unequal number of columns | Dynamic Programming

Title Description

The arrangement of any of 1 to n, then insert the ">" and "<" according to their size relationship between each of the two numbers arranged. Q. In all permutations, how many permutations exactly k "<." The answer to the modulo 2015.

Note: 1 refers to the arrangement of n 1 to n and the n number appears only once in each of the columns.

Input Format

The first line of two integers n, k.

Output Format

An integer that represents the answer.


Now we consider that we have arranged a number n-1, then insert it into the arrangement number nnn nnn

Obviously, there are n n positions to choose from, we first consider the position of either side.

If you insert to the left will cause the new sequence number is greater than one more than the original

If you insert into the far right, it will result in a new serial number less than one more than the original

Is inserted into the other position is greater than or less than a number of position

If the number is greater than the inserted position, by deleting a number greater than, greater than a multiple number smaller than a number, i.e. a multiple number smaller than

If the number is less than the inserted position, by deleting a number less than, greater than a multiple number smaller than a number, greater than a number which is more

We will find a number of insert just over a less-than and less-than the same number in both cases

We DP [i] [j] denotes the number of exactly j i decimal numbers arranged in

Then clearly

dp[i+1][j]+=dp[i][j]*(j+1)

dp[i+1][j+1]+=dp[i][j]*(i-j)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e3+10,mod=2015;
int n,k,dp[N][N];
signed main(){
    cin>>n>>k;
    dp[1][0]=1;
    for(int i=1;i<=n;i++)
    for(int j=0;j<=i;j++){
        (dp[i+1][j]+=dp[i][j]*(j+1))%=mod;
        (dp[i+1][j+1]+=dp[i][j]*(i-j))%=mod;
    }
    cout<<dp[n][k]<<endl;
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/12031812.html