/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 328.0 KiB
#2 Time Exceeded ≥1598ms ≥3.02 MiB
#3 Time Exceeded ≥1600ms ≥5.52 MiB

Code

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int long long
// #define endl "\n"
#define tej ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define sum(zee) (accumulate((zee).begin(), (zee).end(), 0LL))
#define mine(zee) (*min_element((zee).begin(), (zee).end()))
#define maxe(zee) (*max_element((zee).begin(), (zee).end()))
#define mini(zee) (min_element((zee).begin(), (zee).end()) - (zee).begin())
#define maxi(zee) (max_element((zee).begin(), (zee).end()) - (zee).begin())
#define cnt(zee, x) (count((zee).begin(), (zee).end(), (x)))
#define lob(zee, x) (*lower_bound((zee).begin(), (zee).end(), (x)))
#define upb(zee, x) (*upper_bound((zee).begin(), (zee).end(), (x)))
#define ojs() \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);

// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;

#define order_of_key(x) oset.order_of_key(x) // Number of elements less than x
#define find_by_order(k) *oset.find_by_order(k) // K-th smallest element (0-based index)

#define oin(x) oset.insert(x);      // O(log n)
#define oer(x) oset.erase(x);       // O(log n)

int a[1]={0};
class FastInput {
public:
    FastInput() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
    }

    int nextInt() {
        int x;
        cin >> x;
        return x;
    }

    long long nextLong() {
        long long x;
        cin >> x;
        return x;
    }
};

FastInput in;
//ostream &output = ot;
int N;
vector<vector<int>> adj;
vector<bool> used;
vector<int> path;          
bool fd;
void dfs(int depth) {
    if(fd) return; 
    if(depth == N) {
        fd = true;
        return;
    }
    int last = path.back();
    for (int nxt : adj[last]) {
        if(!used[nxt]) {
            used[nxt] = true;
            path.push_back(nxt);
            dfs(depth + 1);
            if(fd) return;
            path.pop_back();
            used[nxt] = false;
        }
    }
}
void solve(int test)
{
    // Your sol here
    N = in.nextInt();
    adj.assign(N+1, vector<int>());
        for (int x = 1; x <= N; x++){
            for (int y = 1; y <= N; y++){
                if(x == y) continue;
                if( abs(x - y) <= 1 ) continue;
                if(__gcd(x, y) != 1) continue;
                adj[x].push_back(y);
            }
        }
        for (int x = 1; x <= N; x++){
            sort(adj[x].begin(), adj[x].end());
        }
 
        bool solfd = false;
        vector<int> bestSol;
        for (int start = 1; start <= N; start++){
            used.assign(N+1, false);
            path.clear();
            used[start] = true;
            path.push_back(start);
            fd = false;
            dfs(1);
            if(fd) {
                solfd = true;
                bestSol = path;
                break;
            }
        }
 
        if(!solfd) {
            cout << -1 << "\n";
        } else {
            for (int x : bestSol)
                cout << x << " ";
            cout << "\n";
        }
}

// Main function
signed main() {
    tej;
    // #ifndef ONLINE_JUDGE
    //     ojs();
    // #endif

    int test=1;
    test=in.nextInt();
    while(test--) {
        solve(test);
    }
    return 0;
}

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 15:46:01
Judged At
2025-06-13 15:46:01
Judged By
Score
2
Total Time
≥1600ms
Peak Memory
≥5.52 MiB