結果

問題 No.2496 LCM between Permutations
ユーザー rniyarniya
提出日時 2023-10-06 23:03:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 4,151 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 207,712 KB
実行使用メモリ 24,396 KB
平均クエリ数 953.72
最終ジャッジ日時 2023-10-06 23:03:25
合計ジャッジ時間 5,963 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,448 KB
testcase_01 AC 26 ms
24,276 KB
testcase_02 AC 25 ms
23,424 KB
testcase_03 AC 26 ms
23,544 KB
testcase_04 AC 27 ms
23,652 KB
testcase_05 AC 26 ms
24,396 KB
testcase_06 AC 27 ms
23,544 KB
testcase_07 AC 27 ms
23,640 KB
testcase_08 AC 27 ms
23,376 KB
testcase_09 AC 27 ms
23,424 KB
testcase_10 AC 27 ms
24,048 KB
testcase_11 AC 26 ms
23,856 KB
testcase_12 AC 26 ms
23,640 KB
testcase_13 AC 25 ms
24,288 KB
testcase_14 AC 27 ms
23,388 KB
testcase_15 AC 31 ms
24,132 KB
testcase_16 AC 35 ms
24,384 KB
testcase_17 AC 36 ms
23,640 KB
testcase_18 AC 92 ms
23,376 KB
testcase_19 AC 74 ms
23,532 KB
testcase_20 AC 95 ms
23,388 KB
testcase_21 AC 84 ms
23,616 KB
testcase_22 AC 80 ms
24,348 KB
testcase_23 AC 104 ms
23,424 KB
testcase_24 AC 89 ms
23,664 KB
testcase_25 AC 115 ms
23,820 KB
testcase_26 AC 115 ms
23,376 KB
testcase_27 AC 112 ms
23,856 KB
testcase_28 AC 114 ms
24,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

/*
1 から N で最大の素数 p
lcm(p, x) は x = 1, p のときのみ p でそれ以外は px
*/

int N;
vector<int> A, B;

int query(int i, int j) {
    assert(0 <= i and i < N);
    assert(0 <= j and j < N);
    cout << "? " << i + 1 << ' ' << j + 1 << endl;
    int res;
#ifdef LOCAL
    res = lcm(A[i], B[j]);
#else
    cin >> res;
#endif
    return res;
}

void answer(vector<int> a, vector<int> b) {
    cout << '!';
    for (int& x : a) cout << ' ' << x;
    for (int& x : b) cout << ' ' << x;
    cout << endl;
#ifdef LOCAL
    assert(a == A and b == B);
#endif
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N;
#ifdef LOCAL
    A.resize(N);
    B.resize(N);
    iota(all(A), 1);
    iota(all(B), 1);
    random_device seed_gen;
    mt19937 engine(seed_gen());
    shuffle(all(A), engine);
    shuffle(all(B), engine);
    debug(A, B);
#endif

    vector<int> A(N, -1), B(N, -1);
    if (N == 1) {
        A[0] = B[0] = 1;
        answer(A, B);
        return 0;
    }
    vector<bool> is_prime(N + 1, true);
    int p = -1;
    for (int i = 2; i <= N; i++) {
        if (not is_prime[i]) continue;
        p = i;
        for (int j = i + i; j <= N; j += i) is_prime[j] = false;
    }
    vector<int> C(N);
    for (int i = 0; i < N; i++) C[i] = query(0, i);
    A[0] = *min_element(all(C));
    debug(C, A[0]);
    int pos = -1;
    if (A[0] == p) {
        pos = 0;
        vector<int> cand;
        for (int i = 0; i < N; i++) {
            if (C[i] == p)
                cand.emplace_back(i);
            else
                B[i] = C[i] / p;
        }
        assert(int(cand.size()) == 2);
        vector<int> D(N);
        for (int i = 0; i < N; i++) D[i] = query(i, cand[0]);
        B[cand[0]] = *min_element(all(D));
        B[cand[1]] = 1 + p - B[cand[0]];
        int one = -1;
        for (int i = 0; i < 2; i++) {
            if (B[cand[i]] == 1) {
                one = cand[i];
            }
        }
        assert(one != -1);
        debug(one);
        for (int i = 1; i < N; i++) A[i] = query(i, one);
    } else {
        for (int i = 0; i < N; i++) {
            if (C[i] == A[0] * p) {
                assert(pos == -1);
                pos = i;
            }
        }
        assert(pos != -1);
        B[pos] = p;
        vector<int> D(N), cand;
        for (int i = 0; i < N; i++) {
            D[i] = query(i, pos);
            if (D[i] == p)
                cand.emplace_back(i);
            else
                A[i] = D[i] / p;
        }
        assert(int(cand.size()) == 2);
        if (query(cand[0], (pos + 1) % N) % p == 0)
            A[cand[0]] = p;
        else
            A[cand[0]] = 1;
        A[cand[1]] = 1 + p - A[cand[0]];
        debug(cand, A);
        int one = -1;
        for (int i = 0; i < 2; i++) {
            if (A[cand[i]] == 1) {
                one = cand[i];
            }
        }
        assert(one != -1);
        debug(one);
        for (int i = 0; i < N; i++) {
            if (B[i] != -1) continue;
            B[i] = query(one, i);
        }
    }
    answer(A, B);
    return 0;
}
0