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. Apache Shiro 使用手冊 链接文件夹整理

    1.Apache Shiro 使用手冊(一)Shiro架构介绍 2.Apache Shiro 使用手冊(二)Shiro 认证 3.Apache Shiro 使用手冊(三)Shiro 授权 4.Apac ...

  2. C++中的构造函数和析构函数

    构造函数: 在类实例化对象时自动执行,对类中的数据进行初始化.构造函数可以从载,可以有多个,但是只能有一个缺省构造函数. 析构函数: 在撤销对象占用的内存之前,进行一些操作的函数.析构函数不能被重载, ...

  3. mysql将一个库中表的某几个字段插入到另一个库中的表

    insert into dbname1.tablename1(filed1,filed2,filed3) select filed1,filed2,filed3from dbname2.tablena ...

  4. 彻底卸载MYSQL,windows版

    转自:http://blog.csdn.net/jasonandwho/article/details/7451310 网上搜的总结帖,直接贴过来的... 由于安装MySQL的时候,疏忽没有选择底层编 ...

  5. Linux设备驱动中断机制

    [主要内容] Linux设备驱动编程中的中断与定时器处理 [正文] 一.基础知识 1.中断 所谓中断是指CPU在执行程序的过程中,出现了某些突发事件急待处理,CPU必须暂停执行当前的程序,转去处理突发 ...

  6. Visual Studio 2012 Ultimate 上安装 Python 开发插件 PTVS

    1.我的环境 操作系统:32位 Win7 旗舰版 Service Pack 1 VS版本:Microsoft Visual Studio Ultimate 2012 版本 11.0.50727.1 R ...

  7. Android - Broadcast机制

    以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luosheng ...

  8. Linux下python升级

    Centos即使用Yum更新也是Python2.6.6所以需要升级到Python2.7.8 1.先下载源码包 1 wget https://www.python.org/ftp/python/2.7. ...

  9. oracle存储过程调试方法

    PL/SQL中为我们提供了[调试存储过程]的功能,可以帮助你完成存储过程的预编译与测试. 点击要调试的存储过程,右键选择TEST 如果需要查看变量,当然调试都需要.在右键菜单中选择Add debug ...

  10. iOS判断UIScrollView的滚动方向

    - (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = scrollView.contentOffset.y; ...