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. Integration testing

    Integration testing 集成测试用来确保app的不同模块之间可以正确的一起工作.ASP.NET Core提供单元测试框架和内建的测试网络服务来支持集成测试,并且测试网络服务不需要网络开 ...

  2. 计蒜客 UCloud 的安全秘钥(随机化+Hash)

    题目链接 UCloud 的安全秘钥 对于简单的版本,我们直接枚举每个子序列,然后sort一下判断是否完全一样即可. #include <bits/stdc++.h> using names ...

  3. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

  4. Codeforces Gym 100431D Bubble Sort 水题乱搞

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  5. 某考试 T1 Hello my friend

    Discription

  6. eclipse不会自动编译的问题解决

    注意:非必要的时候,重新下载eclipse安装是最有效的解决方法. 以下为尝试的步骤: 1.看看project->Build Automatically有没有勾上?如果没有,勾上以后,clean ...

  7. 【CSS】使用CSS控制文字过多自动省略号

    使用CSS可以设置一下样式: <style> u,small{ overflow: hidden; text-overflow: ellipsis; display: -webkit-bo ...

  8. MySQL的1067错误

    1.打开my.ini文件,找到default-storage-engine=InnoDB这一行,把它改成default-storage-engine=MyISAM.*** my.ini必须为ansi格 ...

  9. iOS集成百度地图方法步骤

    前言:app中的导航功能越来越流行,现在我自己做的项目中也有此需求,做过了后记录下笔记.  由于源代码保密所以这里仅仅提供demo,下面是效果图 一:iOS地图SDK 1.打开 百度地图api链接 i ...

  10. 理解Neural Style

    paperA Neural Algorithm of Artistic Style 在艺术领域,尤其是绘画,艺术家们通过创造不同的内容与风格,并相互交融影响来创立独立的视觉体验.如果给定两张图像,现在 ...