結果

問題 No.2496 LCM between Permutations
ユーザー KudeKude
提出日時 2023-10-06 23:27:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 2,999 bytes
コンパイル時間 3,399 ms
コンパイル使用メモリ 275,056 KB
実行使用メモリ 27,428 KB
平均クエリ数 720.28
最終ジャッジ日時 2023-10-06 23:27:21
合計ジャッジ時間 6,922 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
26,772 KB
testcase_01 AC 35 ms
26,672 KB
testcase_02 AC 34 ms
27,212 KB
testcase_03 AC 35 ms
26,916 KB
testcase_04 AC 33 ms
26,844 KB
testcase_05 AC 34 ms
27,292 KB
testcase_06 AC 36 ms
26,836 KB
testcase_07 AC 34 ms
26,788 KB
testcase_08 AC 35 ms
27,156 KB
testcase_09 AC 34 ms
26,832 KB
testcase_10 AC 38 ms
27,308 KB
testcase_11 AC 36 ms
27,040 KB
testcase_12 AC 33 ms
26,568 KB
testcase_13 AC 32 ms
26,788 KB
testcase_14 AC 35 ms
27,272 KB
testcase_15 AC 39 ms
27,080 KB
testcase_16 AC 40 ms
26,976 KB
testcase_17 AC 42 ms
26,848 KB
testcase_18 AC 87 ms
27,324 KB
testcase_19 AC 70 ms
27,028 KB
testcase_20 AC 84 ms
26,692 KB
testcase_21 AC 81 ms
27,216 KB
testcase_22 AC 76 ms
27,204 KB
testcase_23 AC 97 ms
27,324 KB
testcase_24 AC 81 ms
26,696 KB
testcase_25 AC 95 ms
27,008 KB
testcase_26 AC 110 ms
27,148 KB
testcase_27 AC 110 ms
27,428 KB
testcase_28 AC 107 ms
26,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

pair<vector<int>, vector<int>> primes_lpf(const int n) {
  vector<int> primes; primes.reserve(n / 10);
  vector<int> lpf(n + 1);
  for (int i = 2; i <= n; i += 2) lpf[i] = 2;
  for (int i = 3; i <= n; i += 6) lpf[i] = 3;
  if (2 <= n) primes.push_back(2);
  if (3 <= n) primes.push_back(3);
  // 5 * x <= n, x <= floor(n / 5)
  const int n5 = n / 5;
  int x = 5;
  char add_next = 2;
  for (; x <= n5; x += add_next, add_next ^= 2 ^ 4) {
    int px = lpf[x];
    if (px == 0) {
      lpf[x] = px = x;
      primes.push_back(x);
    }
    for (int i = 2;; ++i) {
      int q = primes[i];
      int y = q * x;
      if (y > n) break;
      lpf[y] = q;
      if (q == px) break;
    }
  }
  for (; x <= n; x += add_next, add_next ^= 2 ^ 4) {
    if (lpf[x] == 0) {
      lpf[x] = x;
      primes.push_back(x);
    }
  }
  return {move(primes), move(lpf)};
}

constexpr int PSIZE = 1000000;
auto [primes, lpf] = primes_lpf(PSIZE);

int memo[1010][1010];

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(memo, -1, sizeof(memo));
  int n;
  cin >> n;
  bool flip = false;
  auto ask = [&](int i, int j) {
    if (flip) swap(i, j);
    if (memo[i][j] != -1) return memo[i][j];
    cout << "? " << i + 1 << ' ' << j + 1 << endl;
    int res;
    cin >> res;
    return memo[i][j] = res;
  };
  auto answer = [&](VI a, VI b) {
    if (flip) swap(a, b);
    cout << "!";
    for (int x : a) cout << ' ' << x;
    for (int x : b) cout << ' ' << x;
    cout << endl;
  };
  if (n == 1) {
    answer({1}, {1});
    return 0;
  }
  int pmx = *prev(upper_bound(all(primes), n));
  int ip = -1, jp = -1;
  rep(i, n) {
    if (i == n - 1) {
      ip = jp = n - 1;
      break;
    }
    int r = ask(i, i);
    if (r % pmx == 0) {
      if (ask(i, i + 1) % pmx == 0) {
        ip = i;
      } else {
        jp = i;
      }
      break;
    }
  }
  flip = ip == -1;
  if (flip) swap(ip, jp);
  VI a(n), b(n), oneb;
  rep(j, n) {
    b[j] = ask(ip, j) / pmx;
    if (b[j] == 1) oneb.emplace_back(j);
  }
  assert(oneb.size() == 2);
  int i_test = ip == 0 ? 1 : ip - 1;
  int j1;
  if (ask(i_test, oneb[0]) % pmx == 0) {
    b[oneb[0]] = pmx;
    j1 = oneb[1];
  } else {
    b[oneb[1]] = pmx;
    j1 = oneb[0];
  }
  rep(i, n) a[i] = ask(i, j1);
  answer(a, b);
}
0