Permutation Counting

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  
3661 
3665 
3668 
3669 
3667 
 
题意:
给你一个n和k。问你有多少种数字序列满足有k个数字比该数字对应的下标大。1到n中每个数字出现且出现一次。
比如数字序列3,4,1,2。其中3,4,比对应下标大。
思路:
有点类似错排公式。dp[i][j]表示用到1-i的数字。且有j个数字比下标大的方法数。那么
dp[i][j]=(j+1)*dp[i-1][j]+(i-j)*dp[i-1][j-1]。
公式很简单。当i放在dp[i-1][j]的j个位置或就放在第i个位置时。比下标大的数(E数)不会增加。dp[i][j]=(j+1)*dp[i-1][j]。
而当第i个数放到dp[i-1][j-1]的(i-1)-(j-1)个位置上时。E数会在dp[i-1][j-1]的基础上增加一个。dp[i][j]=(i-j)*dp[i-1][j-1]。
开始看到答案最多为1000000006所以用int存。结果wa了一发。因为在和(j+1)乘时就溢出了。所以要用__int64存。
详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
const int mod=1000000007;
__int64 dp[1010][1010];
int main()
{
int i,j,n,k; for(i=1;i<=1000;i++)
{
dp[i][0]=1;
dp[i][i]=0;
for(j=1;j<i;j++)
dp[i][j]=((j+1)*dp[i-1][j]+(i-j)*dp[i-1][j-1])%mod;
}
while(~scanf("%d%d",&n,&k))
printf("%I64d\n",dp[n][k]);
return 0;
}


hdu 3664 Permutation Counting(水DP)的更多相关文章

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

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

  2. HDU 3664 Permutation Counting (DP)

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

  3. HDU - 3664 Permutation Counting

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

  4. hdu3664 Permutation Counting(dp)

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

  5. hdu 2571 命运(水DP)

    题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j), ...

  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. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  8. hdu 5753 Permutation Bo 水题

    Permutation Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  9. hdu 1241 搬寝室 水dp

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...

随机推荐

  1. Android中app卡顿原因分析示例

    在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. =========== ...

  2. oracle表属性

    1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...

  3. GEOquery

    1)介绍 来自NCBI的Gene Expression Omnibus(GEO)作为各种高通量实验数据的公共存储库. 这些数据包括基于单通道和双通道微阵列的实验,测量mRNA,基因组DNA和蛋白质丰度 ...

  4. Mybatis中的N+1问题与延迟加载

    0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...

  5. ubuntu的文本界面修改字体大小

    使用命令: dpkg-reconfigure console-setup

  6. float数据类型研究,发现其能显示的有效数字极为有限

    1. 范围  float和double的范围是由指数的位数来决定的.  float的指数位有8位,而double的指数位有11位,分布如下:  float:  1bit(符号位) 8bits(指数位) ...

  7. 实验1:c++简单程序设计(1)

    //文中有格式错误请无视 //这个编辑器一言难尽 实验目的 1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构 2. 掌握C++中数据输入和输出的基本方法 ...

  8. 14-n皇后

    /*题目内容: 国际象棋中的皇后可以沿着水平线,垂直线,或者斜线前进,吃掉遇到的所有棋子,如果棋盘上有八个皇后,则这八个皇后如何相安无事的放置在棋盘上,1970年与1971年, E.W.Dijkstr ...

  9. winform弹出文件和目录选择框

    目录选择: FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径&quo ...

  10. dbus通信与接口介绍

    DBUS是一种高级的进程间通信机制.DBUS支持进程间一对一和多对多的对等通信,在多对多的通讯时,需要后台进程的角色去分转消息,当一个进程发消息给另外一个进程时,先发消息到后台进程,再通过后台进程将信 ...