hdu 4489 The King’s Ups and Downs(基础dp)
The King’s Ups and Downs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 582 Accepted Submission(s): 409

or perhaps:
The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are:
For example, if there are four guards: 1, 2, 3,4 can be arrange as:
1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423
For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights.
1 1
2 3
3 4
4 20
2 4
3 10
4 740742376475050
一道较为基础的dp
#include<cstdio>
#include<iostream>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
using namespace std;
LL c[][];
LL a[][];
LL sum[];
void combine(int n);
void dp(int num);
int main()
{
clr(c);
clr(a);
combine();
dp();
int T,n,m;
scanf("%d",&T);
for(int tt=;tt<=T;tt++)
{
scanf("%d%d",&m,&n);
printf("%d %lld\n",m,sum[n]);
}
return ; }
void combine(int n)
{
for(int i=;i<=n;i++)
{
c[i][]=c[i][i]=;
for(int j=;j<i;j++)
{
c[i][j]=c[i][j-]*(i-j+)/j;
}
}
return ;
}
void dp(int num)
{
a[][]=a[][]=a[][]=a[][]=;
sum[]=;
sum[]=;
for(int n=;n<=num;n++)
{
if(n&)
{
for(int i=;i<=n;i++)
if(i&)
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
else
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
sum[n]=a[][n]+a[][n];
}
else
{
for(int i=;i<=n;i++)
if(i&)
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
else
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
sum[n]=a[][n]+a[][n];
}
}
return ;
}
hdu 4489 The King’s Ups and Downs(基础dp)的更多相关文章
- HDU 4489 The King’s Ups and Downs (DP+数学计数)
题意:给你n个身高高低不同的士兵.问你把他们按照波浪状排列(高低高或低高低)有多少方法数. 析:这是一个DP题是很明显的,因为你暴力的话,一定会超时,应该在第15个时,就过不去了,所以这是一个DP计数 ...
- HDU 4489 The King's Ups and Downs
HDU 4489 The King's Ups and Downs 思路: 状态:dp[i]表示i个数的方案数. 转移方程:dp[n]=∑dp[j-1]/2*dp[n-j]/2*C(n-1,j-1). ...
- HDU 4489 The King’s Ups and Downs dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 The King's Ups and Downs Time Limit: 2000/1000 ...
- HDU 4489 The King’s Ups and Downs
http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意:有n个身高不同的人,计算高低或低高交错排列的方法数. 思路:可以按照身高顺序依次插进去. d[i][ ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- UVALive 6177 The King's Ups and Downs
The King's Ups and Downs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UV ...
- The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)
题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324 4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...
- The King’s Ups and Downs
有n个高矮不同的士兵,现在要将他们按高,矮依次排列,问有多少种情况. 化简为 n个人,求出可以形成波浪形状的方法数 #include <iostream> #include <cma ...
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
随机推荐
- Frogs' Neighborhood(POJ1659+Havel-Hakimi定理)
题目链接:http://poj.org/problem?id=1659 题目: 题意:根据他给你的每个点的度数构造一张无向图. 思路:自己WA了几发(好菜啊……)后看到discuss才知道这个要用Ha ...
- 01背包问题的延伸即变形 (dp)
对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...
- bzoj 3207 可持久化线段树
首先因为固定询问长度,所以我们可以将整个长度为n的数列hash成长度为n-k+1的数列,每次询问的序列也hash成一个数,然后询问这个数是不是在某个区间中出现过,这样我们可以根据初始数列的权值建立可持 ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- hasOwnProperty()方法与in操作符
1.hasOwnProperty() 该方法检测属性存在于实例,还是存在于原型,对于存在于实例中的属性则返回true 2.in 使用该操作符时只要通过对象能够访问到的属性都会返回true
- 线程,JSP,Servlet面试题
线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...
- Linux信号函数
1. signal函数: #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); ret-成功返回信号 ...
- Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...
- markdown===在新窗口中打开网址的解决办法,以及其他遗留问题!
[超链接文字](url){:target="_blank"} 遗留问题: 如何设置图片的尺寸 我的复选框一直不生效,why? 公式 $$ 公式 $$ 不生效 如何设置代码块的背景颜 ...
- STL不同容器的使用方法
以下内容摘自:http://blog.csdn.net/u014465639/article/details/70241850 1.vector(需要导入头文件#include <vector& ...