DP (I, J, A, B) indicates the selection of I boys, J girls, the suffix of boys more than girls A (max), girls more than boys B (maximum).
DP (I+1, J, A+1, Max (0, b-1)) + = DP (i, J, A, B)
DP (i, J+1, max (0, A-1), b+1) + = DP (i, J, A, B)
Time complexity O (nmk^2)
-------------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>using namespace std; const int MAXN = 159;const int MAXK =;const int MOD = 12345678;int dp[maxn][maxn][maxk][maxk];int B, G, K;inline void upd (int &x, int t) {if ((x + = t) >= MOD)x-= MOD;}int main () {scanf ("%d%d%d", &b, &g, &k);memset (DP, 0, sizeof DP);dp[0][0][0][0] = 1;for (int i = 0; I <= B; i++)For (int j = 0; J <= G; j + +)for (int a = 0; a <= K; a++)for (int b = 0; b <= K; b++) {upd (dp[i + 1][j][a + 1][max (0, B-1)], dp[i][j][a][b]);upd (dp[i][j + 1][max (0, A-1)][b + 1], dp[i][j][a][b]);}int ans = 0;for (int a = 0; a <= K; a++)for (int b = 0; b <= K; b++)upd (ans, dp[b][g][a][b]);printf ("%d\n", ans);return 0;}
-------------------------------------------------------------------------------------
1037: [ZJOI2008] Birthday party time limit: ten Sec Memory Limit: 162 MB
Submit: 1785 Solved: 1058
[Submit] [Status] [Discuss] Description
Today is Hidadz children's birthday, she invited a lot of friends to attend her birthday party. Hidadz with his friends to the garden, planning to sit in a row to play games. For the game to not be bored, the table should meet the following conditions: For any successive paragraph, the number of boys and girls does not exceed K. Soon, the children found a way to sit down and start the game. Hidadz's good friend Susie found that such a seating plan is actually a lot of, so we quickly found a kind, then how many kinds of it? Hidadz and her friends began to think about the problem ... If there are N boys and M girls in the party, can you answer the questions of Susie and Hidadz? As this number may be many, they only want to know the remainder of this number divided by 12345678.
Input
Contains only a total of 3 integers, the number of boys N, the number of girls m, and the constant K.
Output
Should contain one row for the answer asked in the question.
Sample Input1 2 1Sample Output1HINT
For 100% of data, N, m≤150,k≤20.
Source
Bzoj 1037: [ZJOI2008] birthday party (DP)