結果
問題 | No.2496 LCM between Permutations |
ユーザー | rniya |
提出日時 | 2023-10-06 23:03:19 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 105 ms / 2,000 ms |
コード長 | 4,151 bytes |
コンパイル時間 | 2,391 ms |
コンパイル使用メモリ | 210,732 KB |
実行使用メモリ | 25,220 KB |
平均クエリ数 | 953.72 |
最終ジャッジ日時 | 2024-07-26 16:54:05 |
合計ジャッジ時間 | 5,366 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 27 ms
24,796 KB |
testcase_01 | AC | 19 ms
25,220 KB |
testcase_02 | AC | 20 ms
25,220 KB |
testcase_03 | AC | 20 ms
25,220 KB |
testcase_04 | AC | 21 ms
24,964 KB |
testcase_05 | AC | 20 ms
24,836 KB |
testcase_06 | AC | 21 ms
24,452 KB |
testcase_07 | AC | 23 ms
24,952 KB |
testcase_08 | AC | 23 ms
24,836 KB |
testcase_09 | AC | 22 ms
25,220 KB |
testcase_10 | AC | 23 ms
24,836 KB |
testcase_11 | AC | 24 ms
24,836 KB |
testcase_12 | AC | 22 ms
24,836 KB |
testcase_13 | AC | 22 ms
24,836 KB |
testcase_14 | AC | 22 ms
24,836 KB |
testcase_15 | AC | 25 ms
24,544 KB |
testcase_16 | AC | 26 ms
24,940 KB |
testcase_17 | AC | 28 ms
25,196 KB |
testcase_18 | AC | 89 ms
25,220 KB |
testcase_19 | AC | 69 ms
25,220 KB |
testcase_20 | AC | 94 ms
24,964 KB |
testcase_21 | AC | 82 ms
24,580 KB |
testcase_22 | AC | 72 ms
25,192 KB |
testcase_23 | AC | 97 ms
24,836 KB |
testcase_24 | AC | 84 ms
24,820 KB |
testcase_25 | AC | 104 ms
24,836 KB |
testcase_26 | AC | 105 ms
24,544 KB |
testcase_27 | AC | 104 ms
24,812 KB |
testcase_28 | AC | 102 ms
24,796 KB |
ソースコード
#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; }