Files
python/TangDou/XiTi/1246.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
850 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int t, a, b;
vector<vector<int>> result;
vector<int> path;
//代码随想录,回溯法
void backtracking(int n, int k) { // 定义手中有n个笔记本正在为第k个人发笔记本
if (n == 0 || k == a + 1) {
if (n == 0) result.push_back(path);
return; //没有笔记本可以分,或者没有孩子可以分了,就结束吧
}
//开始分发
for (int i = 0; i <= n; i++) {
path.push_back(i);
//分配给当前人i个然后走向下一个小朋友
backtracking(n - i, k + 1);
path.pop_back();
}
}
int main() {
cin >> t >> a >> b;
//剩余需要分配的个数
int n = t - a * b;
//回溯法
backtracking(n, 1);
cout << result.size() << endl;
return 0;
}