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[][];
for(i=;i<=;i++)
{
dp[i][]=;
dp[][i]=;
} while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=;i<=;i++)
for(j=;j<=;j++)
{ dp[i][j]=(dp[i-][j]*(j+)+dp[i-][j-]*(i-j))%;
}
printf("%d\n",dp[n][k]);
}
}
 

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

  1. hdu 2583 permutation

    permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  3. hdu 5753 Permutation Bo 水题

    Permutation Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  4. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  5. HDU 6628 permutation 1 (暴力)

    2019 杭电多校 5 1005 题目链接:HDU 6628 比赛链接:2019 Multi-University Training Contest 5 Problem Description A s ...

  6. HDU 4345 Permutation dp

    Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...

  7. hdu 5753 Permutation Bo

    这里是一个比较简单的问题:考虑每个数对和的贡献.先考虑数列两端的值,两端的摆放的值总计有2种,比如左端:0,大,小:0,小,大:有1/2的贡献度.右端同理. 中间的书总计有6种可能.小,中,大.其中有 ...

  8. [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 简单完全背包,不多说. #include <cstdio> #include < ...

随机推荐

  1. Android批量图片载入经典系列——使用LruCache、AsyncTask缓存并异步载入图片

    一.问题描写叙述 使用LruCache.AsyncTask实现批量图片的载入并达到下列技术要求 1.从缓存中读取图片,若不在缓存中,则开启异步线程(AsyncTask)载入图片,并放入缓存中 2.及时 ...

  2. 转载:c++内存泄露机制

    对于一个c/c++程序猿来说,内存泄漏是一个常见的也是令人头疼的问题.已经有很多技术被研究出来以应对这个问题,比方 Smart Pointer,Garbage Collection等.Smart Po ...

  3. [Angular 2] Using Two Reducers Together

    Add another reducer: export const SECOND = "SECOND"; export const HOUR = "HOUR"; ...

  4. struts2,hibernate,spring整合笔记(3)

    struts2,hibernate,spring整合笔记(1) struts2,hibernate,spring整合笔记(2) 配好struts和hibernate就要开始spring了 老规矩,还是 ...

  5. [转] .bss段和.data段的区别

    PS:http://stackoverflow.com/questions/16557677/difference-between-data-section-and-the-bss-section-i ...

  6. call和apply区别

    call和apply 基本上是一个意思 区别在于call的第二个参数可以是任意的类型,而apply的第二个参数必须是数组,也可以是arguments.call方法:语法:call(thisObj,Ob ...

  7. js 获取元素在页面上的偏移量的最佳方式

    使用js制作效果时,我们常常要获取某个元素在页面上的偏移量(例如tip提示框功能).而获取偏移量可以直接获取相对于document的偏移量,也可以获取相对与视口的偏移量(viewpoint)加上页面滚 ...

  8. Arcgis Android - HelloWorld

    概述 虽然esri官网上最新版本是10.2.4,但是例子中实在是很难运行,总是出现各种各样的bug.又因为初学是Android,所以不想太浪费时间弄些配置了.决定先将v2.0.0的Arcgis for ...

  9. Javascript 常用函数【2】

    1.常规函数javascript常规函数包括以下9个函数:(1)alert函数:显示一个警告对话框,包括一个OK按钮.(2)confirm函数:显示一个确认对话框,包括OK.Cancel按钮.(3)e ...

  10. log4j的配置及使用

    用日志的好处: 可以长久的保存日志信息. 日志可以保存到:网络.文件.数据库 设置日志的级别. OFF Fatal – System.exit(0); - JVM, ERROR – 错误,模块错误. ...