Ranging from the number of columns

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.

Sample input and output

Input # 1
5 2
Output # 1
66

Description / Tips

For 30% of the data: n <= 10

To 100% of the data: k <n <= 1000

[Problem-solving ideas]

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

Where i and j i is the number of former to have a small number j, j <= i-1

How to understand this equation? ?

Number of columns to a length of inserting a number i, then a total of i + 1 can be inserted into position (the first position and last position of the intermediate positions i-1). Since the insertion of a number greater than all the previous number, it is less than the number of the position of the insert this number will be more out of a greater-than, the number is less than the number will remain unchanged in the original stream, if the insertion is greater than the number of positions will be one less than the number and inserted in the head position is also a multiple greater than the end position of a plurality less than the total the number less than the number of positions with a constant (j + 1), an increase of the number of remaining (ij) is less than the number of positions will

【code】

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 int n,k,f[1005][1005]; 
 6 const int mod=2015;
 7 int main(){
 8     ///freopen("2401.in","r",stdin);
 9     //freopen("2401.out","w",stdout);
10     scanf("%d%d",&n,&k);
11     for (register int i=1;i<=n;i++)
12         f[i][0]=1;
13     for (register int i=2;i<=n;i++)
14         for (register int j=1;j<=k;j++)
15             f[i][j]=(f[i-1][j-1]*(i-1-j+1)+f[i-1][j]*(j+1))%mod;
16     printf("%d\n",f[n][k]);
17     return 0;
18 }

 

Guess you like

Origin www.cnblogs.com/66dzb/p/11257394.html