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. ubuntu16.04安装ibus中文输入法

    按照网上给的安装方法,没成功,在切换ibus的时候总是报错,记录下解决办法. 安装语言包 System Settings–>Language Support–>Install/Remove ...

  2. configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.

    configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+. 一.错误发生情景: 在安装gcc时,执行.c ...

  3. hdu 1186(搜索+HASH)

    方程的解数 Time Limit: 15000MS   Memory Limit: 128000K Total Submissions: 7045   Accepted: 2417 Case Time ...

  4. 从 React Router 谈谈路由的那些事

    React Router 是专为 React 设计的路由解决方案,在使用 React 来开发 SPA (单页应用)项目时,都会需要路由功能,而 React Router 应该是目前使用率最高的. Re ...

  5. 微信小程序踩坑之一【weui-wxss-master单选按钮图标修改思路】

    小程序原生所带的weui框架做小程序UI实在太方便了,但是他的一些细微变化也是让开发中碰到不少头疼的问题 一直以来单选多选的美化都是设计师重点表达的地方之一 而weui-wxss-master中的单选 ...

  6. Loj #125. 除数函数求和(2)

    link : https://loj.ac/problem/125 分块calc即可. #include<bits/stdc++.h> #define ll long long using ...

  7. ie下li标签中span加float:right不换行问题解决方案

    在IE6,IE7下使用标签时,在加入右浮动样式(float:right)后,会换行的bug解决方案:bug案例:新闻列表中,为使时间右对齐,加右浮动产生换行 <ul> <li> ...

  8. electron入门教程

    1.atom/electron github: https://github.com/atom/electron 中文文档: https://github.com/atom/electron/tree ...

  9. ios 6.0模拟器页面调出pop窗口消失后无法使用键盘

    ios 6模拟器上,点击事件调用出pop窗口,这个窗口新创建了window,在pop窗口消失的函数中使用了makeKeyWindow,这个是将要显示的window放到最前端.发现 屏蔽这个方法后可以了 ...

  10. 老毛桃winpe优盘启动系统个性修改全攻略

    PE优盘系统也有很多:大白菜.老毛桃.深度.通用PE工具箱.U大师.电脑店……这些PE优盘系统大多都会捆绑软件安装.更改主页等,一不小心,你就中招.虽然有些是可以自己去取消,但是启动画面还是带有各种L ...