比赛 20220531高一小测验 评测结果 AAAAAAAAAA
题目名称 素数环 最终得分 100
用户昵称 lihaoze 运行时间 0.793 s
代码语言 C++ 内存使用 2.94 MiB
提交时间 2022-06-01 20:46:24
显示代码纯文本
#include <bits/stdc++.h>

int primes[][9] = {{1}, {1, 2, 4, 6, 10, 12, 16, 18}, {0, 1, 3, 5, 9, 11, 15, 17}, {0, 2, 4, 8, 10, 14, 16, 20}, {1, 3, 7, 9, 13, 15, 19}, {0, 2, 6, 8, 12, 14, 18}, {1, 5, 7, 11, 13, 17}, {0, 4, 6, 10, 12, 16}, {3, 5, 9, 11, 15}, {2, 4, 8, 10, 14, 20}, {1, 3, 7, 9, 13, 19}, {0, 2, 6, 8, 12, 18, 20}, {1, 5, 7, 11, 17, 19}, {0, 4, 6, 10, 16, 18}, {3, 5, 9, 15, 17}, {2, 4, 8, 14, 16}, {1, 3, 7, 13, 15}, {0, 2, 6, 12, 14, 20}, {1, 5, 11, 13, 19}, {0, 4, 10, 12, 18}, {3, 9, 11, 17}};
bool is[] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0};

const int N = 20;
int n, cnt;
bool st[N];
std::vector<int> chosen(1);

void dfs(int a) {
	if (a == n) {
		for (int i = 1; i <= n; ++ i) if (!st[i]) chosen.emplace_back(i);
		int ed = chosen.back();
		if (!is[ed + chosen[1]] || !is[ed + *(chosen.end() - 2)]) return void(chosen.pop_back());
		for (auto i = ++ chosen.begin(); i != chosen.end(); ++ i) std::cout << *i << ' ';
		return std::cout << '\n', void(chosen.pop_back());
	}
	for (int i = 0; i <= 8; ++ i) {
		int x = primes[chosen.back()][i];
		if (x > n || !x || st[x]) continue;
		chosen.emplace_back(x), st[x] = true;	
		dfs(a + 1);
		chosen.pop_back(), st[x] = false;
	}
}

int main() {
	freopen("primering.in", "r", stdin); 
	freopen("primering.out", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	while (std::cin >> n) {
		memset(st, 0, sizeof st);
		chosen.clear(), chosen.emplace_back(0);
		std::cout << "Case " << ++ cnt << ":\n";
		dfs(1);
		std::cout << '\n';
	}
	return 0;
}