E - Permutation Descent Counts(DP)

7787 Permutation Descent Counts
Given a positive integer, N, a permutation of order N is a one-to-one (and thus onto) function from
the set of integers from 1 to N to itself. If p is such a function, we represent the function by a list of
its values:
[p(1)p(2). . . p(N)]
For example,
[5 6 2 4 7 1 3] represents the function from {1 . . . 7} to itself which takes 1 to 5, 2 to 6, . . ., 7 to 3.
For any permutation p, a descent of p is an integer k for which p(k) > p(k + 1). For example, the
permutation [5 6 2 4 7 1 3] has a descent at 2(6 > 2) and 5(7 > 1).
For permutation p, des§ is the number of descents in p. For example, des([5624713]) = 2. The
identity permutation is the only permutation with des§ = 0. The reversing permutation with p(k) =
N + 1 − k is the only permutation with des§ = N − 1.
The permutation descent count (PDC) for given order N and value v is the number of permutations
p of order N with des§ = v. For example:
P DC(3, 0) = 1{[123]}
P DC(3, 1) = 4{[132], [213], [231], 312]}
P DC(3, 2) = 1{[321]}
Write a program to compute the PDC for inputs N and v. To avoid having to deal with very large
numbers, your answer (and your intermediate calculations) will be computed modulo 1001113.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by the
integer order, N (2 ≤ N ≤ 100), followed by an integer value, v (0 ≤ v ≤ N − 1).
Output
For each data set there is a single line of output. The single output line consists of the data set number,
K, followed by a single space followed by the PDC of N and v modulo 1001113 as a decimal integer.
Sample Input
4
1 3 1
2 5 2
3 8 3
4 99 50
Sample Output
1 4
2 66
3 15619
4 325091

题意:给出n和v,找1到n这n个数逆序对的数量是v的方式有多少种

二维数组dp[i][j],i代表有n个数,j代表数量v
状态转移方程: dp[i][j]=dp[i-1][j-1](i-j)+dp[i-1][j](j+1)
比如求(5,3),可以由(4,3)和(4,2)推出,如果是(4,2)4个数的时候有5个位置可以插入数,因为有2个逆序对,所以插入后还为2个逆序对的位置有3个,即插入到2个逆序对之间和插入到最后,为j-1+1个,所以插入后逆序对变成3的位置就有i-j个;
如果是(4,3)有5个位置科插入,插入后逆序对数不变的应该是3+1个,即j+1。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f
#define memset(a,n) memset(a,n,sizeof(a))
#define PI acos(-1)
const int MAX=1e5+10;
using namespace std;
ll dp[105][105];

int main()
{
    int t,i,n,v;
    dp[1][0]=1;
    dp[1][1]=0;

    for(ll i=2; i<=100; i++)
        for(ll j=0; j<=i-1; j++)
        {
             if(j==0||j==i-1)
                dp[i][j]=1;
             else
                dp[i][j]=(dp[i-1][j-1]*(i-j)+dp[i-1][j]*(j+1))%1001113;
        }
    cin>>t;
    while(t--)
    {
        cin>>i>>n>>v;
        cout<<i<<" "<<dp[n][v]%1001113<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值