Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- template <typename T>
- istream& operator >> (istream& in, vector <T> &a) {
- for (T&i : a) {
- in >> i;
- }
- return in;
- }
- template <typename T>
- ostream& operator << (ostream& out, vector <T> &a) {
- for (T&i : a) {
- out << i << " ";
- }
- return out;
- }
- bool check(int a, int b) {
- if (a == 0 || b == 0) {
- return true;
- }
- return a % b == 0 || b % a == 0;
- }
- bool operator > (const vector <int> &a, const vector <int> &b) {
- if (a.size() != b.size()) {
- return a.size() > b.size();
- }
- for (int i = 0; i < a.size(); ++i) {
- if (a[i] != b[i]) {
- return a[i] > b[i];
- }
- }
- return false;
- }
- vector <int> ans;
- int cnt = 0;
- bool f(vector <int> &v, int i, int n, vector <int> &used) {
- if (v > ans) {
- ans = v;
- }
- if (i == n) {
- cout << v << "\n";
- return true;
- }
- for (int j = n - 1; j >= 0; --j) {
- if (!used[j]) {
- if (i == 0 || check(v[i - 1], j)) {
- v.push_back(j);
- used[j] = 1;
- if (f(v, i + 1, n, used)) {
- return true;
- }
- used[j] = 0;
- v.pop_back();
- }
- }
- }
- ++cnt;
- return false;
- }
- int main() {
- int t;
- cin >> t;
- for (int n = 2; n <= t; ++n) {
- vector <int> v, used(n, 0);
- cout << n << ":\n";
- f(v, 0, n, used);
- cout << ans << endl;
- cout << cnt << endl;
- cnt = 0;
- ans.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement