题意:把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。
分析:枚举每个盘子可以放的苹果数都是0~M,最后去重就可以了。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 100000 + 10; const int MAXT = 10000 + 10; using namespace std; int m, n; vector<int> tmp; vector<int> a[MAXN]; int cnt; void dfs(int now, int id, int sum){ if(sum > m) return; if(id == n){ if(sum == m){ int t = 0; sort(tmp.begin(), tmp.end()); for(int i = 0; i < cnt; ++i){ bool ok = false; for(int j = 0; j < n; ++j){ if(a[i][j] != tmp[j]){ ok = true; break; } } if(ok) ++t; } if(t == cnt){ for(int i = 0; i < n; ++i){ a[cnt].push_back(tmp[i]); } ++cnt; } } return; } for(int i = 0; i <= m; ++i){ tmp.push_back(now + i); dfs(now + i, id + 1, sum + now + i); vector<int>::iterator it; for(it = tmp.begin(); it != tmp.end(); ++it){ if(*it == now + i){ tmp.erase(it); break; } } } } int main(){ int T; scanf("%d", &T); while(T--){ cnt = 0; tmp.clear(); for(int i = 0; i < MAXN; ++i){ a[i].clear(); } scanf("%d%d", &m, &n); dfs(0, 0, 0); printf("%d\n", cnt); } return 0; }
来源:https://www.cnblogs.com/tyty-Somnuspoppy/p/6151551.html