【URAL - 1114 】Boxes (dp,组合数学)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83685005

题干:

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 NA and B separated by space.

Output

The result of your program must be an integer written on the only line of output.

Example

input output
2 1 1
9

解题报告:

   这题首先将问题做了一步简化(没想到啊,还是太菜)就是将红球和蓝球分开处理了,那么我们可以先看全是红球的,再看全是蓝球的,最后相乘不就是结果吗?换句话说,我们只需要打表往盒子里装 球 就可以了。最后需要用哪个的结果直接去表中找不就好了吗?

   dp[i][j]表示前i个盒子装入j个球的方案数。。。最后两者相乘,就是答案、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
unsigned ll dp[22][20];
int n,m;
int main() 
{
	dp[0][0]=1;
	for(int i = 0; i<=15; i++) dp[1][i] = 1;
	for(int i = 2; i<=22; i++) {
		for(int j = 0; j<=15; j++) {
			for(int k = 0; k<=j; k++) {
				dp[i][j] += dp[i-1][k];
			}
		}
	}
	int n,a,b;
	cin>>n>>a>>b;
	unsigned ll ans1 = 0,ans2 = 0;
	for(int i = 0; i<=a; i++) ans1 += dp[n][i];
	for(int i = 0; i<=b; i++) ans2 += dp[n][i];	
	printf("%llu\n",ans1*ans2);//printf("%I64u\n",ans1*ans2);
	return 0 ;
}

总结:

  其实不应该想不到这第一步化简的。。因为你想啊不然这题不就没法做了吗,,,盒子不同,球的颜色不同,但是同色的不同球又相同、、这么多关系上哪找dp去?所以化简了之后就是,不同盒子放入相同球的放球问题了。、、

   这题dp的状态设计还可以这样(wjh想):dp[i][j]表示截止第i个盒子,第i个盒子中放入j个相同球的方法数。。本来我觉得这肯定不对啊因为你肯定得记录一下一共放了多少球了啊。他说不用,因为先读入了a和b的值,所以相当于把后面的球数空出来就可以了,也就是内层循环for(int k = 0; k<=j-a; k++)我觉得也可以啊、、 但是仔细一想这样是行不通的,,会多算了一些方案数,因为你这个状态是没啥问题,,但是你用的值得dp[i-1][]的吧,这个就只表示了第i-1个盒子装的,而前面第i-2个...之类的就没有考虑到。也就是有很多不能算进去的状态都算进去了。所以会多算。。

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83685005
今日推荐