hdu 3664 dp

原创 2015年11月17日 21:33:58

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">Permutation Counting</span>
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1501    Accepted Submission(s): 764


Problem 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 0 3 1
 

Sample Output
1 4
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}
 

Source



题意:对于任一种N的排列A,定义它的E值为序列中满足A[i]>i的数的个数。给定N和K(K<=N<=1000),问N的排列中E值为K的个数。

解法:简单DP(orz kuangbin)。。。。dp[i][j]表示i个数的排列中E值为j的个数。假设现在已有一个E值为j的i的排列,对于新加入的一个数i+1,将其加入排列的方法有三:1)把它放最后,加入后E值不变    2)把它和一个满足A[k]>k的数交换,交换后E值不变       3)把它和一个不满足A[k]>k的数交换,交换后E值+1      根据这三种方法得到转移方程dp[i][j] = dp[i - 1][j] + dp[i - 1][j] * j + dp[i - 1][j - 1] * (i - j);


代码

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define F first
#define S second
#define PI acos(-1.0)
#define E  exp(1.0)
#define INF 0xFFFFFFF
#define MAX -INF
#define len(a) (__int64)strlen(a)
#define mem0(a) (memset(a,0,sizeof(a)))
#define mem1(a) (memset(a,-1,sizeof(a)))
using namespace std;
__int64 gcd(__int64 a, __int64 b) {
return b ? gcd(b, a % b) : a;
}
__int64 lcm(__int64 a, __int64 b) {
return a / gcd(a, b) * b;
}
__int64 max(__int64 a, __int64 b) {
return a > b ? a : b;
}
__int64 min(__int64 a, __int64 b) {
return a < b ? a : b;
}
long long dp[1010][1010];
const long long mod = 1000000007;
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int n, k;
for (int i = 1; i <= 1000; i++) {
dp[i][0] = 1;
for (int j = 1; j < i; j++)
dp[i][j] = (dp[i - 1][j] + dp[i - 1][j] * j
+ dp[i - 1][j - 1] * (i - j)) % mod;
}
while (scanf("%d%d", &n, &k) != EOF) {
printf("%I64d\n", dp[n][k]);
}
return 0;
}



版权声明:与有肝胆人共事,从无字句处读书

hdu 3664Permutation Counting(简单dp)

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • opm777
  • opm777
  • 2013年11月01日 15:09
  • 1022

HDU 3664 Permutation Counting(DP)

Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the am...

HDU 3664 Permutation Counting (DP)

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...

HDU 3664 Permutation Counting (DP)

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...

计数dp-hdu-3664-Permutation Counting

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3664 题目意思: 求1~n的排列个数,使得逆序数(ai>i ) 为给定的k. 解题思路:...

hdu 3664(第二欧拉数,HIT2011春省赛选拔赛通过,hdu G++ 46MS, C++ 31MS通过)

今天很悲剧的省赛选拔赛,早上我们几个都是只做出了海狸啃木头那道博弈,下午倒是还可以,不过相对来讲实验这边基本功还是比不过计院那边扎实啊!跟计院的同学相比实力差了不少。下午的B题对应的是hdu 3664...

HDU DP 题集

  • 2013年04月24日 18:31
  • 724KB
  • 下载

(解题报告)POJ3664---Election Time---用结构体实现两组相关联数据的排序问题

两次快排 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u SubmitStatus De...

ZOJ Problem Set - 3664 Split the Rectangle

The 2012 ACM-ICPC Asia Changchun Regional Contest-J 题目大一就是去掉某一些使得两个点在同一矩形内,然后问有还剩多少矩形。 实际就...
  • KQZXCMH
  • KQZXCMH
  • 2012年10月16日 16:41
  • 601

poj-3664

#include #include #include using namespace std; #define COW_NUM 50000 class cow_vote {...
  • fyfcauc
  • fyfcauc
  • 2014年06月26日 15:34
  • 330
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 3664 dp
举报原因:
原因补充:

(最多只允许输入30个字)