hdu 2583 permutation 动态规划

Problem Description

Permutation plays a very important role in Combinatorics. For example ,1 2 3 4  5 and 1 3 5 4 2 are both 5-permutations. As everyone‘s known, the number of n-permutations is n!. According to their magnitude relatives ,if we insert the sumbols "<" or ">"between every pairs of consecutive numbers of a permutations,we can get the permutations with symbols. For example,1 2 3 4 5 can be changed to 1<2<3<4<5, 1 3 5 4 2 can be changed to 1<3<5>4>2. Now it‘s yout task to calculate the number of n-permutations with k"<"symbol. Maybe you don‘t like large numbers ,so you should just geve the result mod 2009.

Input

Input may contai multiple test cases. Each test case is a line contains two integers n and k .0<n<=100 and 0<=k<=100. The input will terminated by EOF.

Output

The nonegative integer result mod 2007 on a line.

Sample Input

5 2

Sample Output

66

题意大概就是求n的全排列中,用大于小于号连接,其中小于号有k个的情况有多少种。注意,ac代码是%2009,不是2007

显然n的全排列有n!个,考虑下dp算法

状态转移是DP(n,k)=dp(n-1,k)*(k+1)+dp(n-1)(k-1)*(n-k)

n代表考虑n个数字的状态,k代表小于号的个数。

先作特殊情况,序列1 2 3 4 5 6,一共5个小于号,如果我要加入数字7,那么我有7种加法,并且只有一种加法会使小于号+1.

序列a1 a2 a3 a4 a5 ……a(n-1),一共有n-1个数字,有k个小于号,那么我加入an有n种加法,其中会使小于号+1的有n-k种(就是加在大于号的位置上),其他的k+1种不会改变小于号的个数。

#include<stdio.h>
int main()
{
    int n,k,i,j,s;
    int dp[111][111];
    for(i=0;i<=101;i++)
    {
        dp[i][0]=1;
        dp[0][i]=0;
    }  

    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(i=1;i<=101;i++)
            for(j=1;j<=101;j++)
            {

                dp[i][j]=(dp[i-1][j]*(j+1)+dp[i-1][j-1]*(i-j))%2009;
            }
printf("%d\n",dp[n][k]);
    }
}

hdu 2583 permutation 动态规划

时间: 2025-01-07 22:57:48

hdu 2583 permutation 动态规划的相关文章

HDU 4917 Permutation

题意: 一个序列p1.p2.p3--pn是由1.2.3--n这些数字组成的  现给出一些条件pi<pj  问满足所有条件的排列的个数 思路: 很容易想到用一条有向的线连接所有的pi和pj  那么就构成了有向无环图(题中说有解所以无环) 又因为pi各不相同  那么题目就变成了有向无环图的拓扑排序的种类数 题目中边数较少  所以可能出现不连通情况  我们先讨论一个连通集合内拓扑排序的种类数 题目中m较小  可以利用状压后的记忆化搜索解决 现在考虑如果知道了A和B两个集合各自的种类数  如果把它们合起

HDU 4917 Permutation 拓扑排序的计数

题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以只一张有向无环图.这样子,我们就可以把排列计数的问题转化为一个图的拓扑排序计数问题. 拓扑排序的做法可以参见ZJU1346 . 因为题目中点的数量比较多,所以无法直接用状压DP. 但是题目中的边数较少,所以不是联通的,而一个连通块的点不超过21个,而且不同连通块之间可以看做相互独立的.所以我们可以对

hdu 3853 LOOPS 动态规划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 迷宫类的动态规划 首先要作个数学推导 假设留在原地.右移.下移的概率分别是a, b, c 用dp[i][j]表示在第i行第j格能走出去的期望步数 则有: dp[i][j] = a * (dp[i][j] + 1) + b * (dp[i][j+1] + 1) + c * (dp[i+1][j] + 1) 整理一下可得: dp[i][j] = 1/(1-a) * (a +  b * (dp[i]

【数学】HDU 5753 Permutation Bo

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 题目大意: 两个序列h和c,h为1~n的乱序.h[0]=h[n+1]=0,[A]表示A为真则为1,假为0. 函数f(h)=(i=1~n)∑ci[hi>hi−1 && hi>hi+1] 现在给定c的值,求f(h)的期望. 题目思路: [数学] 头尾的概率为1/2,中间的概率为1/3,直接求和. 1 // 2 //by coolxxx 3 // 4 #include<io

HDU 5481 Desiderium 动态规划

Desiderium Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5481 Description There is a set of intervals, the size of this set is n. If we select a subset of this set with equal probability, how many the expected

[HDU 1114] Piggy-Bank (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 简单完全背包,不多说. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using

[HDU 3535] AreYouBusy (动态规划 混合背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个,属性为1的为至多完成1个,属性为2的为任意完成. 每个任务做完后都有个价值,问在T个时间单位内完成n个任务集合的任务获得的最大价值是多少?如果不能满足要求输出-1 首先先分析什么情况下输出-1: 因为属性为0的代表至少要完成1个,当遇到一个属性为0的任务集合里一个都无法完成的时候,输出-1. 其他

百度之星资格赛 hdu 4826 Labyrinth 动态规划

/********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能走一格,且仅仅能向上向下向右走曾经没有走过的格子,每个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币能够为负,须要给强盗写欠条),度度熊刚開始时身上金币数为0.问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T

HDU 4345 Permutation dp

Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 404 Problem Description There is an arrangement of N numbers and a permutation relation that alter one