/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 1ms 540.0 KiB
#3 Wrong Answer 2ms 592.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> ans;
            for (ll i = 2; i <= n; i += 2) ans.push_back(i);
            for (ll i = n - (n % 2 == 0); i >= 1; i -= 2) ans.push_back(i);

            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:31:25
Judged At
2025-06-13 16:31:25
Judged By
Score
2
Total Time
2ms
Peak Memory
592.0 KiB