1114. Boxes
Time limit: 0.6 second
Memory limit: 64 MB
Memory limit: 64 MB
N boxes are lined up in a sequence (1 ≤ N ≤ 20). You have A red balls and B blue balls (0 ≤ A ≤ 15, 0 ≤ B
≤ 15). The red balls (and the blue ones) are exactly the same. You can
place the balls in the boxes. It is allowed to put in a box, balls of
the two kinds, or only from one kind. You can also leave some of the
boxes empty. It's not necessary to place all the balls in the boxes.
Write a program, which finds the number of different ways to place the
balls in the boxes in the described way.
Input
Input contains one line with three integers N, A and B separated by space.
Output
The result of your program must be an integer written on the only line of output.
Sample
input | output |
---|---|
2 1 1 |
9 |
Problem Source: First competition for selecting the Bulgarian IOI team.
Tags: none
Difficulty: 191 Printable version Submit solution Discussion (30)
My submissions All submissions (8069) All accepted submissions (2464) Solutions rating (2022)
My submissions All submissions (8069) All accepted submissions (2464) Solutions rating (2022)
思路:dp;
dp[n][i][j]表示放前n个盒子时,前n个盒子中的红球为i个,蓝球为j个的方案数,状态转移方程dp[n][i][j]=sum(dp[n-1][x][y])(x<=i&&y<=j);
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 typedef unsigned long long LL; 8 LL dp[40][40][40]; 9 int main(void) 10 { 11 int i,j,k; 12 int n,m; 13 while(scanf("%d %d %d",&k,&n,&m)!=EOF) 14 { 15 memset(dp,0,sizeof(dp)); 16 int x,y,z; 17 dp[0][0][0]=1; 18 int xx; 19 int yy; 20 LL sum=0; 21 for(i=1; i<=k; i++) 22 { 23 for(x=0; x<=n; x++) 24 { 25 for(y=0; y<=m; y++) 26 { 27 for(xx=0; xx<=x; xx++) 28 { 29 for(yy=0; yy<=y; yy++) 30 { 31 dp[i][x][y]+=dp[i-1][xx][yy]; 32 } 33 } 34 if(i==k) 35 sum+=dp[i][x][y]; 36 } 37 } 38 } 39 printf("%llu ",sum); 40 } 41 return 0; 42 }