HDU 3664 Permutation Counting(DP)
来源:互联网 发布:西单有mac专柜吗 编辑:程序博客网 时间:2024/05/21 06:32
Description
Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the amount of elements where ai > i. For example, the E-value of permutation {1, 3, 2, 4} is 1, while the E-value of {4, 3, 2, 1} is 2. You are requested to find how many permutations of {1, 2, …, N} whose E-value is exactly k.
Input
There are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N).
Output
Output one line for each case. For the answer may be quite huge, you need to output the answer module 1,000,000,007.
Sample Input
3 03 1
Sample Output
14
Hint
There is only one permutation with E-value 0: {1,2,3}, and there are four permutations with E-value 1: {1,3,2}, {2,1,3}, {3,1,2}, {3,2,1}
题意:给出N,一个数列中包含从1到N的N个数,现在让这个数列进行重排,当ai>i时,这个元素对数列的E—value值贡献1。
问:给出N和k(数列的E—value值),问初始数列有多少种重排方法,使其E—value值为k。
思路:先列出1-3的的所有情况
N\K 0 1 2 3
1 1 0
2 1 1 0
3 1 4 1 0
当 n为2 ,k为 0 时有一种排列(1,2),k为1 时有一种排列(2,1)
当 n为3 ,k为 1 时只需在n为2,k为 0 和 1 的情况下插入3使E—value值加1或不变即可……
当n为2, k为 0 时,有 两个位置对E-value没有贡献,把3插入没有贡献的位置,把之前的数放到数列最后,这样E-value值则为1,有2种方法。
当n为2, k为 1 时,当前k值为所求E-value值,所有把3插入有贡献的位置,把之前的数放到最后,这样有1种方法。另外把3放到最后也是一种方法,所以有2种方法。
因此得到递推方程d[i][j]=d[i-1][j]*(j+1)+d[i-1][j-1]*(i-j)
细节参见代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<algorithm>#include<queue>#include<cmath>#include<sstream>using namespace std;typedef long long int LL;int n,k;LL d[1005][1005];int mod=1e9+7;int main(){ memset(d,0,sizeof(d)); d[1][0]=1;d[1][1]=0; for(int i=1;i<=1001;i++) d[i][0]=1; for(int i=2;i<=1001;i++){ for(int j=1;j<=1001;j++) d[i][j]=((d[i-1][j]*(j+1))%mod+(d[i-1][j-1]*(i-j))%mod)%mod; } while(~scanf("%d%d",&n,&k)){ printf("%I64d\n",d[n][k]); } return 0;}
0 0
- Hdu 3664 Permutation Counting DP
- HDU 3664 Permutation Counting (DP)
- HDU 3664 Permutation Counting (DP)
- HDU 3664 Permutation Counting(DP)
- hdu-3664 Permutation Counting ,dp
- 计数dp-hdu-3664-Permutation Counting
- hdu 3664 Permutation Counting(水DP)
- hdu 3664Permutation Counting(简单dp)
- hdu 3664 Permutation Counting 简单dp
- HDU 3664 Permutation Counting (DP)
- HDU 3664 Permutation Counting (DP)
- HDU 3664 Permutation Counting (DP)
- HDU 3664 Permutation Counting
- HDOJ 3664 Permutation Counting / UVALive 5092 DP
- HDU 3667 Permutation Counting
- hdoj 3664 Permutation Counting
- HDOJ 3664 Permutation Counting
- HDOJ 3664 Permutation Counting
- Http状态code(PHP开发)
- Centos7安装显卡驱动
- ArcEngine的ToolbarControl解析
- resin与tomcat之比较
- SQL一些常用的查询语句
- HDU 3664 Permutation Counting(DP)
- 智应能用来发财吗?
- hdu 4451
- 压力测试中的一些关键概念:集合点,事务,检查点,思考时间。
- 项目经理面试题
- hdu1179Ollivanders: Makers of Fine Wands since 382 BC.(二分匹配)
- PHP新人的第一篇博客 ~~~~~~环境的配置与导入MySQL数据库~~~
- POJ3122
- HBase的协处理器编码实战