HDU3664 Permutation Counting

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

2010 Asia Regional Harbin

题意:对于任一种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 = 1010;
const int MOD = 1000000007;

ll dp[maxn][maxn];
int n, k;

void init() {
    dp[1][0] = 1;
    dp[1][1] = 0;
    for (int i = 1; i <= 1000; i ++) {
        for (int j = 0; j <= 1000; j ++) {
            dp[i+1][j] = (dp[i+1][j] % MOD + (j + 1) * dp[i][j] % MOD) % MOD;
            dp[i+1][j+1] = (dp[i+1][j+1] % MOD + (i - j) * dp[i][j] % MOD) % MOD;
        }
    }
}

int main() {

    init();
    while (~scanf("%d %d", &n, &k)) {
        printf("%d\n", dp[n][k]);
    }

    return 0;
}
时间: 2024-08-05 12:01:12

HDU3664 Permutation Counting的相关文章

uva 1485 - Permutation Counting(递推)

题目链接:uva 1485 - Permutation Counting 题目大意:给定n和k,要求求一个由1~n组成的序列,要求满足ai>i的i刚好有k个的序列种数. 解题思路:dp[j][i]表示长度为i,j个位置满足的情况. dp[j+1][i]+=dp[j][i]?(j+1); 1, (3), (4), 2: 括号位置代表ai>i,既满足位置,此时i = 4, j = 2. -> 1, (3), (4), 2, 5 1 种,在最后追加 -> 1, (5), (4), 2,

UVA - 1485 Permutation Counting

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

HDU 3664 Permutation Counting (DP)

题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i 个排列,恰好有 j 个数,dp[i][j] = dp[i-1][j] * (j+1) + dp[i-1][j-1] * (i-j).这是状态转移方程. 为什么是这样呢,dp[i-1][j] * (j+1) 意思是,你前i-1个已经凑够 j 个了,那么我把 i 可以去替换这个 j 个任何一个,再加上,把这

组合计数&#183;经典序列问题

1. LA 5092 Permutation Counting 题意:给定$1\sim n$的排列$\{a_1, a_1,..., a_n\}$,满足$a_i > i $的下标$i$的个数称为此排列的$E$值, 例如$\{1,3,2,4\}$的$E$值为$1$,$\{4, 3, 2, 1\}$的$E$值为$2$,给定整数$n$和$k(1 \leq n \leq 1000, 0 \leq k \leq n)$, 求$E$值恰好为$k$的排列个数. 分析:我们可以这样从$1\sim (i-1)$的排

UVALive 5971

Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it doesn't have x and x+1 appearing consecutively, where (1 ≤ x < N)  For example, for N=3 , all goodpermutations are: 1. {1, 3, 2} 2.{2, 1, 3} 3.{3, 2,

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

HDU 3664 递推

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1794    Accepted Submission(s): 955 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val

Find the missing element in a given permutation.

Task description A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: