/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 10ms 4.27 MiB
#2 Wrong Answer 12ms 4.27 MiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

const int N = 1e6 + 7;
vector<int> spf(N); // SPF : smallest prime factor

void sieve() { //=> O(n log(log(n)))
    for (int i = 1; i < N; i++) {
        spf[i] = i;
    }
    for (int i = 2; i * i < N; i++) {
        if (spf[i] == i) {
            for (int j = i * i; j < N; j += i) {
                if (spf[j] == j) spf[j] = i;
            }
        }
    }
}

void prime_factors (ll x) {
    while (x != 1) {
        cout << spf[x] << " ";
        x /= spf[x];
    }
    cout << endl;
}

void solve() {
    int n;
    cin >> n;
    if(n <= 4 or n == 6) {
        cout << -1 << endl;
        return;
    }
    multiset<array<int, 3>, greater<>> st; // {dist prime cnt, val, i}
    for(int i = 1; i <= n; i++) {
        set<int> tmp;
        int x = i;
        while (x != 1) {
            tmp.insert(spf[x]);
            // cout << spf[x] << " ";
            x /= spf[x];
        }
        int val = 1;
        for(auto &p: tmp) val *= p;
        // cout << i << " => " << val << endl;
        st.insert({tmp.size(), val, i});
    }
    vector<int> Ans(n), Pf(n);
    auto [cnt, val, ans] = *st.begin();
    Ans[0] = ans, Pf[0] = val;
    st.erase(st.begin());
    int c = 1;
    while(c < n) {
        array<int, 3> tmp = {-1, -1, -1};
        for(auto &[cnt1, val, ans]: st) {
            if(__gcd(Pf[c - 1], val) == 1 && abs(ans - Ans[c - 1]) > 1) {
                tmp = {cnt1, val, ans};
                break;
            }
        }
        if(tmp[0] == -1) {
            cout << -1 << endl;
            return;
        }
        Pf[c] = tmp[1];
        Ans[c] = tmp[2];
        ++c;
    }
    for(auto &i: Ans) cout << i << " "; cout << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    sieve(); // build <==
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1203 D. Roy Loves Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-11 20:47:52
Judged At
2025-06-11 20:47:52
Judged By
Score
0
Total Time
12ms
Peak Memory
4.27 MiB