HDU 4489 The King’s Ups and Downs dp
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4489
The King’s Ups and Downs
Memory Limit: 32768/32768 K (Java/Others)
样例输入
4
1 1
2 3
3 4
4 20
样例输出
1 1
2 4
3 10
4 740742376475050
题意
给你n个数(1到n),问排出高低高,,,或低高低,,,的所有情况数。
题解
考虑最后一个数n摆放的位置,枚举在它左边放x个数,右边放n-1-x个数,然后转移。
需要用到一个结论:对于任意的n>=2,高低高和低高低的情况数都是相等的!
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=22;
LL dp[maxn],C[maxn][maxn];
void get_C(){
C[0][0]=1;
for(int i=1;i<maxn;i++){
C[i][0]=1;
for(int j=1;j<=i;j++){
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
}
void pre(){
get_C();
dp[0]=dp[1]=1;
dp[2]=2;
for(int i=3;i<=20;i++){
for(int x=0;x<i;x++){
LL lef=x<=1?dp[x]:dp[x]/2;
int y=i-x-1;
LL rig=y<=1?dp[y]:dp[y]/2;
dp[i]+=C[i-1][x]*lef*rig;
}
}
}
int main() {
pre();
int tc;
scanf("%d",&tc);
while(tc--){
int kase,n;
scf("%d%d",&kase,&n);
prf("%d %lld\n",kase,dp[n]);
}
return 0;
}
//end-----------------------------------------------------------------------