Permutation Counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1487    Accepted Submission(s): 754

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[i][j]表示前i个数的排列中E值为j的个数,所以当插入第i+1个数时,如果放在第i+1或满足条件的j个位置时,j不变,与其余i-j个位置上的数调换时j会+1。所以
dp[i+1][j] = dp[i+1][j] + (j + 1) * dp[i][j];
dp[i+1][j+1] = dp[i+1][j+1] + (i - j) * dp[i][j];
 
/*
ID: LinKArftc
PROG: 3664.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int MOD = ; ll dp[maxn][maxn];
int n, k; void init() {
dp[][] = ;
dp[][] = ;
for (int i = ; i <= ; i ++) {
for (int j = ; j <= ; j ++) {
dp[i+][j] = (dp[i+][j] % MOD + (j + ) * dp[i][j] % MOD) % MOD;
dp[i+][j+] = (dp[i+][j+] % MOD + (i - j) * dp[i][j] % MOD) % MOD;
}
}
} int main() { init();
while (~scanf("%d %d", &n, &k)) {
printf("%d\n", dp[n][k]);
} return ;
}
 

HDU3664 Permutation Counting的更多相关文章

  1. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  2. hdu 3664 Permutation Counting(水DP)

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

  3. HDU - 3664 Permutation Counting 排列规律dp

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

  4. HDU - 3664 Permutation Counting

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

  5. HDU 3664 Permutation Counting (DP)

    题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...

  6. HDU 6880 Permutation Counting dp

    题意: 给你一个n和一个长度为n-1的由0/1构成的b序列 你需要从[1,n]中构造出来一个满足b序列的序列 我们设使用[1,n]构成的序列为a,那么如果ai>ai+1,那么bi=1,否则bi= ...

  7. UVALive 5971

    Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...

  8. LeetCode_Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. CH3602 Counting Swaps

    题意 3602 Counting Swaps 0x30「数学知识」例题 背景 https://ipsc.ksp.sk/2016/real/problems/c.html Just like yeste ...

随机推荐

  1. 第七篇Python基本数据类型之数字&字符串&布尔值

    数字 写在最前,必须要会的:int() 整型 Python3里无论数字多长都用int表示,Python2里有int和Long表示,Long表示长整型 有关数字的常用方法,方法调用后面都必须带括号() ...

  2. Win10下Pytorch的安装和使用[斗之力三段]

    简介: 看到paper的代码是用Pytorch实现的,试图理解代码,但是看不懂,只能先学一些基础教程来帮助理解.笔记本电脑配置较低,所以安装一个没有CUDA的版本就可以了.安装完之后,就可以跟着教程边 ...

  3. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...

  4. BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图

    Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i]]这么多段长度为1时间中选出一个时间进行抢劫,并计划抢 ...

  5. 数据结构7——BFS

    一.重拾关键 宽度优先搜索,也有称为广度优先搜索,简称BFS.类似于树的按层次遍历的过程. 初始状态:图G所有顶点均未被访问过,任选一点v. 遍历过程:假设从图中某顶点v出发,在访问了v之后依次访问v ...

  6. Manacher算法——最长回文子串

    一.相关介绍 最长回文子串 s="abcd", 最长回文长度为 1,即a或b或c或d s="ababa", 最长回文长度为 5,即ababa s="a ...

  7. hadoop自定义数据类型

    统计某手机数据库的每个手机号的上行数据包数量和下行数据包数量 数据库类型如下: 数据库内容如下: 下面自定义类型SimLines,类似于平时编写的model import java.io.DataIn ...

  8. 【EasyNetQ】- 订阅

    EasyNetQ订阅者订阅消息类型(消息类的.NET类型).一旦通过调用Subscribe方法设置了对类型的订阅,就会在RabbitMQ代理上创建一个持久队列,并且该类型的任何消息都将被放置在队列中. ...

  9. 51nod 1680区间求和 (dp+树状数组/线段树)

    不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...

  10. 移动开发:美团外卖Android Lint代码检查实践

    概述 Lint是Google提供的Android静态代码检查工具,可以扫描并发现代码中潜在的问题,提醒开发人员及早修正,提高代码质量.除了Android原生提供的几百个Lint规则,还可以开发自定义L ...