uva 702 The Vindictive Coach (DP)

来源:互联网 发布:ed hardy正品网络购买 编辑:程序博客网 时间:2024/06/05 09:01

Problem A : The Vindictive Coach

From:UVA, 702


The Vindictive Coach

The coach of a football team, after suffering for years the adverse comments of the media about his tactics, decides to take his revenge by presenting his players in a line-up in such a way that the TV cameras would be compelled to zigzag in a ridiculous bobbing motion, by alternating taller and shorter players. However, the team captain objects that he must be the first of the line by protocolary reasons,and that he wants to be seen in the best possible light: that is, he should not have a taller colleague nest to him unless there is no alternative (everyone else is taller than him). Even in this case, the height difference should be as small as possible, while maintaining the zigzag arrangement of the line.

With this condition the coach addresses an expert in computation (i.e. you) to help him find the number of different alignments he may make, knowing that all players have a different height. They are always numbered by stature starting by 1 as the shortest one. Of course the number of players may be arbitrary, provided it does not exceed 22.

Input

It is a set of lines, each of which contains two positive integers N andm separated by a blank space. $N (\le 22)$ represents the number of players in the line-up andm the captain's number, who as told is always the first of the line.

Output

For every line of the input a line with positive integer indicating the number of possible alignments under the above conditions.

Sample Input

3 13 34 1

Sample Output

111
#include <iostream>#include <cstdio>using namespace std;const int maxn = 30;long long int dp[maxn][maxn][2];int N , m;void initial(){for(int i = 0;i < maxn;i++){for(int j = 0;j < maxn;j++){dp[i][j][0] = -1;dp[i][j][1] = -1;}}}long long DP(int h , int l , int k){if(h+l == 0){return 1;}if((h <= 0 && k == 1) || (l <= 0 && k == 0)){return 0;}if(dp[h][l][k] != -1){return dp[h][l][k];}long long int ans = 0;if(k == 0){for(int i = 1;i <= l;i++){ans += DP(h+i-1 , l-i , 1);}}else{for(int i = 1;i <= h;i++){ans += DP(h-i , l+i-1 , 0);}}dp[h][l][k] = ans;return ans;}void computing(){if(N <= 2){cout << 1 << endl;return;}if(m == 1){cout << DP(N-3 , 1 , 0) << endl;}else{cout << DP(N-m , m-1 , 0) << endl;}}int main(){while(cin >> N >> m){initial();computing();}return 0;}


0 0
原创粉丝点击