跳到正文

题解

题解:P10448 组合型枚举

lkrqwq约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
2
题目
P10448

版本

  1. v1当前

双倍经验:P1157

同组合的输出,我们只需用 dfs 选择 mm 个数并输出即可,时间复杂度 O(Cnm)O(C_{n}^{m})。

代码如下:

C++
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int a[45];
void dfs(int n, int m, int step, int lst) {
	if (step > m) {
		for (int i = 1; i <= m; i++) {
			cout << a[i] << " ";
		}
		cout << endl;
		return;
	}
	for (int i = lst + 1; i <= n; i++) {
		a[step] = i;
		dfs(n, m, step + 1, i);
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	dfs(n, m, 1, 0);
}
原文