[计数dp] ural 1114. Boxes
2014-05-09 15:15
232 查看
题目链接:
http://acm.timus.ru/problem.aspx?space=1&num=1114
Time limit: 0.6 second
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.
Problem Source: First competition for selecting the Bulgarian IOI team.
Tags: none (
hide tags for unsolved problems
)
Difficulty: 194 Printable version Submit solution Discussion
(29)
My submissions All submissions (7145) All
accepted submissions (2115) Solutions rating (1803)
题目意思:
有n个盒子,有相同的A球a个,相同的B球b个,每个盒子可以不放球可以放多个球,球可以不全放完。求放的种数。
n<=20 a,b<=15
解题思路:
简单计数dp
dp[i][j][k]:表示1~i个盒子中放了j个A球,k个B球的总的种数。
显然dp[i][j][k]=∑(dp[i-1][jj][kk]) (jj<=j,kk<=k)
注意要用usigned long long不然最后一个会超
代码:
http://acm.timus.ru/problem.aspx?space=1&num=1114
1114. Boxes
Time limit: 0.6 secondMemory 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 |
Tags: none (
hide tags for unsolved problems
)
Difficulty: 194 Printable version Submit solution Discussion
(29)
My submissions All submissions (7145) All
accepted submissions (2115) Solutions rating (1803)
题目意思:
有n个盒子,有相同的A球a个,相同的B球b个,每个盒子可以不放球可以放多个球,球可以不全放完。求放的种数。
n<=20 a,b<=15
解题思路:
简单计数dp
dp[i][j][k]:表示1~i个盒子中放了j个A球,k个B球的总的种数。
显然dp[i][j][k]=∑(dp[i-1][jj][kk]) (jj<=j,kk<=k)
注意要用usigned long long不然最后一个会超
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define ull unsigned long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 22 ull dp[Maxn][Maxn][Maxn]; int n,a,b; void init() { memset(dp,0,sizeof(dp)); dp[0][0][0]=1; for(int i=1;i<=20;i++) { for(int j=0;j<=15;j++) { for(int k=0;k<=15;k++) { for(int jj=0;jj<=j;jj++) for(int kk=0;kk<=k;kk++) dp[i][j][k]+=dp[i-1][jj][kk]; } } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); init(); while(~scanf("%d%d%d",&n,&a,&b)) { //printf("%I64d\n",dp [a][b]); ll ans=0; for(int i=0;i<=a;i++) for(int j=0;j<=b;j++) ans+=dp [i][j]; printf("%I64u\n",ans); } return 0; }
相关文章推荐
- ural 1114,计数dp
- URAL1918--Titan Ruins: Artful Manipulations-----计数DP(被别人写的博客坑死了)
- ural 1018 Binary Apple Tree(树形DP)建二叉树
- ZJU 1346 Comparing Your Heroes 状态压缩DP 拓扑排序的计数
- hdoj2151简单的计数DP
- ural1057(数位dp)
- URAL 1018 Binary Apple Tree(树DP)
- URAL 1167. Bicolored Horses(二维dp或者记忆化搜索)
- 【BZOJ 1833】 [ZJOI2010]count 数字计数|数位DP
- URAL 1036 Lucky Tickets 【dp】【高精度】
- URAL 1183 Brackets Sequence(DP)
- ural 1057. Amount of Degrees(数位DP,4级)
- ural 1005 dp dfs or 0 1背包
- URAL 1225 Flags 简单DP,一重循环
- Ural 1009 K-based Numbers(DP)
- URAL 1519 基础插头DP
- Ural1081 (dp)
- URAL 1091 Tmutarakan Exams (DP或容斥)
- ural1039 树形dp
- URAL 2018. The Debut Album (dp)