HDOJ 4689 Derangement DP
来源:互联网 发布:传智播客c语言第四期 编辑:程序博客网 时间:2024/06/11 23:03
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.
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
113
Author
Zejun Wu (watashi)
Source
2013 Multi-University Training Contest 9
#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;}
0 0
- HDOJ 4689 Derangement DP
- HDOJ 4689: Derangement
- HDU 4689 Derangement【DP】
- hdu 4689 Derangement(dp)
- HDU 4689 Derangement【DP递推】【好题】【思维题】
- HDU 4689 Derangement
- hdu 4689 Derangement
- Derangement 问题
- Gym 100971B Derangement
- 文章标题 Gym100971B :Derangement
- HDOJ 1949 Tourist(DP)
- hdoj 1500 Chopsticks(DP)
- hdoj 1208 dp
- HDOJ Dividing DP
- HDOJ FATE 二维DP
- HDOJ - 2375 状态DP..
- HDOJ - 4502 简单DP..
- HDOJ 1011 树形DP
- shell中如何截取字符串(2)
- 只有原创和翻译文章才能推荐到首页
- Vlan互联的配置和排障
- Java webservice 搭建
- POJ 2135 Farm Tour(费用流)
- HDOJ 4689 Derangement DP
- KVM虚拟机网络配置 Bridge方式
- vim使用方法简介
- 【C++】cstring里面的函数简介
- nyoj-766-回文数
- dedecms织梦5.7SP1登录后台操作无反应之卡死解决办法
- Objective-C之成魔之路【12-分类与协议】
- 从学习海洋里的迷茫到认清方向
- Linux /bin, /sbin, /usr/bin, /usr/sbin区别