Http://acm.timus.ru/problem.aspx? Space = 1 & num = 1114
There are n boxes and balls of two colors. The red balls and the basketball balls are A and B respectively. Now, you can freely put the ball into the box. Each box can be in one color, two colors. How many methods are available.
Set DP [I] [J] [k] to indicate that J red balls and K basketball balls are left in the I-th box. The state transition equation can be listed: DP [I] [J] [k] = Σ (DP [I-1] [JJ] [Kk]) (j <= JJ <= A and K <= KK <= B ).
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <bitset>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64//#define LL long long#define ULL unsigned long long#define eps 1e-9#define PI acos(-1.0)using namespace std;ULL dp[25][20][20];int main(){ int n,A,B; while(~scanf("%d %d %d",&n,&A,&B)) { memset(dp,0,sizeof(dp)); dp[0][A][B] = 1; ULL ans = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j <= A; j++) { for(int k = 0; k <= B; k++) { for(int jj = j; jj <= A; jj++) { for(int kk = k; kk <= B; kk++) dp[i][j][k] += dp[i-1][jj][kk]; } if(i == n ) ans += dp[i][j][k]; } } } printf("%I64u\n",ans); } return 0;}
Ural 1114. Boxes (DP)