排列组合(深搜专题复习)

有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB","BA"两种。 

Input

每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。

Output

对应每组数据输出排列数。(任何运算不会超出2^31的范围)

Sample Input

2 2
1 1

Sample Output

2

注意多组输入:

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>


const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int n,m;
int a[maxn];
int s;
void dfs(int x)
{
	if(x==0)
	{
		s++;
		return ;
	}
	for(int t=0;t<n;t++)
	{
		if(a[t]>0)
		{
			a[t]--;
			dfs(x-1);
			a[t]++;
		}
	}
}
int main()
{
	while(cin>>n>>m)
	{
		s=0;
		for(int t=0;t<n;t++)
		{
			scanf("%d",&a[t]);
		}
		dfs(m);
		cout<<s<<endl;
	}
	return 0;
 } 
原文地址:https://www.cnblogs.com/Staceyacm/p/10781767.html