/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 556.0 KiB
#2 Wrong Answer 1ms 540.0 KiB
#3 Wrong Answer 1ms 584.0 KiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
#define memo(a,b) memset(a,b,sizeof(a))

ll n;
const ll N=2e3+5;
vector<ll> res;
bool used[N];

bool check(ll x, ll y) {
    return __gcd(x, y) == 1 && abs(x - y) > 1;
}

bool dfs(ll len) { 
    // 
    if (len == n) return 1;
    for (ll i = 1; i <= n; i++) {
        if (used[i]) continue;
        if (len == 0 || check(res[len - 1], i)) {
            used[i] = 1;
            res.push_back(i);
            if (dfs(len + 1)) return 1;
            res.pop_back();
            used[i] = 0;
        }
    }
    return 0;
}

bool isok(vector<ll> &v) {
    for (ll i = 1; i < v.size(); i++) {
        if (__gcd(v[i], v[i-1]) != 1 || abs(v[i] - v[i-1]) <= 1) return 0;
    }
    return 1;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int test; cin >> test;
    while (test--) {
        cin >> n;
        res.clear(); memo(used, 0);
        if (n <= 9) {
            if (dfs(0)) {
                for (auto x : res) cout <<x<<" ";
                cout << endl;
            } else {
                cout << -1 << endl;
            }
        } else {
            vector<ll> odd, even;
            for (ll i = 1; i <= n; i++) {
                if (i & 1) odd.push_back(i);
                else even.push_back(i);
            }
            reverse(odd.begin(), odd.end());
            vector<ll> ans = odd;
            for (auto x : even) ans.push_back(x);

            if (isok(ans)) {
                for (auto x : ans) cout <<x<<" ";
                cout << endl;
            } else cout << -1 << endl;
        }
    }
}

Information

Submit By
Type
Submission
Problem
P1203 D. Roy Loves Permutation
Contest
Brain Booster #10
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-13 16:29:12
Judged At
2025-06-13 16:29:12
Judged By
Score
2
Total Time
1ms
Peak Memory
584.0 KiB