Permutation Descent Counts(递推)

1968: Permutation Descent Counts

Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 123     Solved: 96


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([5 6 2 4 7 1 3]) = 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:

PDC(3, 0) = 1 { [ 1 2 3 ] }
PDC(3, 1) = 4 { [ 1 3 2 ], [ 2 1 3 ], [ 2 3 1 ], 3 1 2 ] }
PDC(3, 2) = 1 { [ 3 2 1 ] }`

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

Hint

Source

2017湖南多校第十三场

//题意:给出 n,v 求 1 -- n 的排列中,相邻的数,出现 v 次前面数比后面数大的种数。

题解:假如设 dp[i][j] 为 1 -- i 的排列,出现 j 次前面数比后面数大的情况的种数,那么

递推,有两个来源,dp[i-1][j] 和 dp[i-1][j-1] ,只要考虑 i 放置的位置即可,分清楚情况讨论清楚即可!

比赛时没想清楚唉!

 1 # include <cstdio>
 2 # include <cstring>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <vector>
 6 # include <queue>
 7 # include <stack>
 8 # include <map>
 9 # include <bitset>
10 # include <set>
11 # include <cmath>
12 # include <algorithm>
13 using namespace std;
14 #define lowbit(x) ((x)&(-x))
15 #define pi acos(-1.0)
16 #define eps 1e-8
17 #define MOD 1001113
18 #define INF 0x3f3f3f3f
19 #define LL long long
20 inline int scan() {
21     int x=0,f=1; char ch=getchar();
22     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();}
23     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();}
24     return x*f;
25 }
26 inline void Out(int a) {
27     if(a<0) {putchar(‘-‘); a=-a;}
28     if(a>=10) Out(a/10);
29     putchar(a%10+‘0‘);
30 }
31 #define MX 105
32 //Code begin...
33 int dp[MX][MX];
34
35 void Init()
36 {
37     dp[1][0]=1;
38     for (int i=2;i<=100;i++)
39     {
40         for (int j=0;j<=i-1;j++)
41         {
42             dp[i][j] = dp[i-1][j]*(j+1)%MOD;
43             if (j!=0)
44                 dp[i][j] = (dp[i][j]+dp[i-1][j-1]*(i-j))%MOD;
45         }
46     }
47 }
48
49 int main()
50 {
51     Init();
52     int t = scan();
53     while (t--)
54     {
55         int c = scan();
56         int n = scan();
57         int m = scan();
58         printf("%d %d\n",c,dp[n][m]);
59     }
60     return 0;
61 }

时间: 2024-10-07 19:47:05

Permutation Descent Counts(递推)的相关文章

uva 1485 - Permutation Counting(递推)

题目链接:uva 1485 - Permutation Counting 题目大意:给定n和k,要求求一个由1~n组成的序列,要求满足ai>i的i刚好有k个的序列种数. 解题思路:dp[j][i]表示长度为i,j个位置满足的情况. dp[j+1][i]+=dp[j][i]?(j+1); 1, (3), (4), 2: 括号位置代表ai>i,既满足位置,此时i = 4, j = 2. -> 1, (3), (4), 2, 5 1 种,在最后追加 -> 1, (5), (4), 2,

UVA 11077 Find the Permutations 递推置换

                           Find the Permutations Sorting is one of the most used operations in real life, where Computer Science comes into act. It iswell-known that the lower bound of swap based sorting is nlog(n). It means that the best possiblesor

Code Force 429B Working out【递推dp】

Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and mcolumns. Let number a[i][j] represents the calories burned by performing workout at the

Gym10081 A - Arcade Game -康托展开、全排列、组合数变成递推的思想

最近做到好多概率,组合数,全排列的题目,本咸鱼不会啊,我概率论都挂科了... 这个题学到了一个康托展开,有点用,瞎写一下... 康托展开: 适用对象:没有重复元素的全排列. 把一个整数X展开成如下形式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[2]*1!+a[1]*0![1] 其中a[i]为当前未出现的元素中是排在第几个(从0开始),并且0<=a[i]<i(1<=i<=n) 用来求全排列中这个串排第几,康托展开的逆运算就是

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

hdu 1267 递推

下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4326    Accepted Submission(s): 2268 Problem Description 2005年11月份,我们学校参加了ACM/ICPC 亚洲赛区成都站的比赛,在这里,我们获得了历史性的突破,尽管只是一枚铜牌,但获奖那一刻的激动,也许将永远铭刻

hdu 2067(递推或卡特兰数【待补充】)

//解法一:递推#include<iostream> using namespace std; long long d[36][36]; int main() { for(int i=1;i<=35;i++) { d[0][i]=1; } for(int i=1;i<=35;i++) for(int j=i;j<=35;j++) { if(i==j) d[i][j]=d[i-1][j]; else d[i][j]=d[i-1][j]+d[i][j-1]; } int n; i

NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m.但是大家来的时间不同,随机次序来机房,带电脑的人直接准备比赛而没带电脑的人需要向带电脑并还没和别人公用的人求助(当然会答应).但是,如果不存在带电脑并还没和别人公用的人,那他就要等了,等是很让人头疼的,这就不和谐了,当然假如没有这样的情况发生比赛是很和谐的. Input 输入多组数据,每组数据只有一行m(

矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

https://www.vijos.org/p/1067 很容易推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵,其中右上角的(k-1)*(k-1)的矩阵是单位矩阵,第k行的每个数分别对应f[n-1],f[n-2],,f[n-k]的系数.然后构造一个k*1的矩阵,它的第i行代表f[i],是经过直接递推得到的.设ans[][]是第一个矩阵的n-k次幂乘上第二个矩阵,f[n]就是ans[k][1]. 注意:用__int64 #in