欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
一、问题:
Description
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(p) is the number of descents in p. For example, des([5624713]) = 2. The identity permutation is the only permutation with des(p) = 0. The reversing permutation with p(k) = N + 1 − k is the only permutation with des(p) = 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(p) = 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
二、题意:
第一行数字 T 代表测试样例个数。
接下来的 T 行每行第一个数 cnt 表示序号,第二个数 n,第三个数v。
求:1~n的全排列中,逆序对数为v的情况数。
三、思路:
dp[ i ][ j ] 表示1~ i 的全排列中,逆序对数为 j 的情况数,首先当 j =0 时,表示无逆序对,则dp[ i ] [ 0 ] = 1,即只有“顺序”一种。
其他情况,dp[ i ][ j ]的来源只有两种:
1)dp[ i-1 ][ j-1 ],表示第 j 个数恰好也构成了逆序。
2)dp[ i-1 ][ j ] ,表示截至第 j-1 个数时逆序对数就已经为 j ,并且第 j 个不构成逆序。
状态转移方程为:dp[ i ][ j ] = dp[ i-1 ][ j-1 ] * ( i-j ) + dp[ i-1 ][ j ] * ( j+1 ) ;
由于结果可能比较大,所以还需要mod 1001113 。
四、代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-10
#define mod 1e9+7
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,0,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define fork(a,b) for(int k=a;k<=b;k++)
#define ifor(a,b) for(int i=a;i>=b;i--)
#define jfor(a,b) for(int j=a;j>=b;j--)
#define kfor(a,b) for(int k=a;k>=b;k--)
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
using namespace std;
typedef long long LL;
const int MOD=1001113;
int main()
{
int dp[105][105],T,cnt,n,v;
read(T);
mem(dp,0);
fori(1,100)
dp[i][0]=1;
fori(1,100)
forj(1,i-1)
dp[i][j]=(dp[i-1][j-1]*(i-j)%MOD+dp[i-1][j]*(j+1)%MOD)%MOD;
while(T--)
{
scanf("%d %d %d",&cnt,&n,&v);
printf("%d %d\n",cnt,dp[n][v]);
}
return 0;
}
欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~