Permutations
Problem Description
There are n kinds of goods, and know the number of each item. Required number of permutations of m selected items therefrom. For example, there are two items A, B, and the number is 1, 2 from the selected items are arranged the "AB", "BA" two kinds.
Input
Each group has two data input lines, the first line is two numbers n, m (1 <= m, n <= 10), indicates the number of items, the second row has the number n, the n denote the number of items .
Output
Each number corresponding to a data output arrangement. (Any operation are not exceeded 2 ^ 31)
Sample Input
2 2 1 1
Sample Output
2
AC Code
#include<bits/stdc++.h> using namespace std; int n,m,s,a[11]; void dfs(int x){ if(x==0){ s++;return; } for(int i=0;i<n;++i){ if(a[i]){ a[i]--; dfs(x-1); a[i]++; } } } int main(){ while(cin>>n>>m) { s=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } dfs(m); cout<<s<<endl; } return 0; }
dfs If only find the solution does not require backtracking
If you need to find all the solutions you need