DP具体解释见:

http://blog.csdn.net/liguan1/article/details/10468139

Derangement

Time Limit: 7000/7000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)

Total Submission(s): 846    Accepted Submission(s): 256

Problem Description
A derangement is a permutation such that none of the elements appear in their original position. For example, [5, 4, 1, 2, 3] is a derangement of [1, 2, 3, 4, 5]. Subtracting the original permutation from the derangement, we get the derangement difference [4,
2, -2, -2, -2], where none of its elements is zero. Taking the signs of these differences, we get the derangement sign [+, +, -, -, -]. Now given a derangement sign, how many derangements are there satisfying the given derangement sign?
 
Input
There are multiple test cases. Process to the End of File.

Each test case is a line of derangements sign whose length is between 1 and 20, inclusively.
 
Output
For each test case, output the number of derangements.
 
Sample Input
+-
++---
 
Sample Output
1
13
 
Author
Zejun Wu (watashi)
 
Source
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; typedef long long int LL; LL dp[50][50];
char str[50]; int main()
{
while(cin>>str)
{
if(str[0]=='-')
{
puts("0"); continue;
}
memset(dp,0,sizeof(dp));
int n=strlen(str);
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
if(str[i-1]=='+')
{
if(j) dp[i][j]+=dp[i-1][j-1];
dp[i][j]+=dp[i-1][j]*j;
}
else if(str[i-1]=='-')
{
dp[i][j]+=dp[i-1][j]*j;
dp[i][j]+=dp[i-1][j+1]*(j+1)*(j+1);
}
}
}
cout<<dp[n][0]<<endl;
}
return 0;
}

HDOJ 4689 Derangement DP的更多相关文章

  1. HDU-4689 Derangement DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4689 题意:初始序列1,2...n,求所有满足与初始序列规定大小的错排数目.. 这道题目感觉很不错~ ...

  2. HDOJ -- 4632 区间DP

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  3. hdoj 1087 (DP)

    代码: #include<iostream>   #include<cmath>   using namespace std;  int a[1005], dp[1005];  ...

  4. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  5. [HDOJ]Coin Change(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2069 题意 有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔 ...

  6. [dp]HDOJ4960 Another OCD Patient

    题意: 给一个n, 第二行给n堆的价值v[i], 第三行给a[i].  a[i]表示把i堆合在一起需要的花费. 求把n堆变成类似回文的 需要的最小花费. 思路: ①记忆化搜索 比较好理解... dp[ ...

  7. 找规律/数位DP HDOJ 4722 Good Numbers

    题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...

  8. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  9. 数学+dp HDOJ 5317 RGCDQ

    题目传送门 /* 题意:给一个区间,问任意两个数的素数因子的GCD最大 数学+dp:预处理出f[i],发现f[i] <= 7,那么用dp[i][j] 记录前i个f[]个数为j的数有几个, dp[ ...

随机推荐

  1. linux信号------探步

    前言 Linux以进程为单位来执行程序.我们可以 将计算机看作一个大楼,内核(kernel)是大楼的管理员,进程是大楼的房客.每个进程拥有一个独立的房间(属于进程的内存空间),而每个房间都是不允 许该 ...

  2. 从反汇编看待C++ new

    首先来看最简单的new操作 int main() { int *temp = new int; delete temp; } 反汇编结果:调用了operator new 00311C9E push 4 ...

  3. GridControl CardView ShowCardExpandButton or GridCardExpandButton

    关于DevExpress.XtraGrid.v13.1.dll和DevExpress.XtraGrid.v12.2.dll中ShowCardExpandButton  或者 GridCardExpan ...

  4. POJ 2577: Interpreter

    简略解题报告 Description A certain computer has 10 registers and 1000 words of RAM. Each register or RAM l ...

  5. 在Eclipse中打开Hadoop工程

    1. 安装虚拟机,我用的是VMware Workstation 12 Player 2. 在VM中安装Ubuntu,我用的镜像文件是ubuntu-15.10-desktop-amd64.iso 3. ...

  6. RichEditControl(富文本控件)

    可以发邮 件??? https://ww w.evget.com/article/2014/3/25/20723.html

  7. hdu6223(后缀数组)

    题意: 给一个长度为n的字符串s[0..n-1],但i的后继不再是i+1,而是(i*i+1)%n,求所有长度为n的“子串”中,字典序最大的是谁 n<=150000 分析: 如果是一般的字符串,那 ...

  8. RabbitMQ 消费端限流、TTL、死信队列

    目录 消费端限流 1. 为什么要对消费端限流 2.限流的 api 讲解 3.如何对消费端进行限流 TTL 1.消息的 TTL 2.队列的 TTL 死信队列 实现死信队列步骤 总结 消费端限流 1. 为 ...

  9. layDate 日期与时间组件 入门

    首先第一步 在官方下载layDate文件.layUI官网:http://layer.layui.com/    https://www.layui.com/laydate/ layDate文件的下载步 ...

  10. 7.【nuxt起步】-Nuxt与后端数据交互

    接下来就是对接服务端接口,展示真实的数据 1.做了个虚拟接口地址:http://test.yms.cn/testjson.asp 输出数据: { "title": "单间 ...