跳到正文

题解

题解:AT_nyc2015_11 チーム戦

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

本题可以转化为求有 33 个 00,n−3n−3 个 11 的序列的全排列。

如果当前位是 11 就输出,否则不输出。

实现只需要用 prev_permutation 即可。

时间复杂度: O(nCn3)O(nC_n^3) 。

CODE:

C++
#include <cstdio>
#include <algorithm>
using namespace std;
int n, num=0;
int f[25];
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= 3; ++i)
		f[i] = 1;
	do {
        num++;
	} while(prev_permutation(f + 1, f + n + 1));
    printf("%d\n", num);
    do {
		for(int i = 1; i <= n; ++i)
			if(f[i]) printf("%d ", i);
		putchar('\n');
	} while(prev_permutation(f + 1, f + n + 1));  // 注意是 prev,因为初始排列的编号是最大的。
}
原文