結果

問題 No.2496 LCM between Permutations
コンテスト
ユーザー Kude
提出日時 2023-10-06 23:27:14
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,999 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,980 ms
コンパイル使用メモリ 360,660 KB
実行使用メモリ 31,656 KB
平均クエリ数 720.28
最終ジャッジ日時 2026-04-03 17:43:23
合計ジャッジ時間 4,969 ms
ジャッジサーバーID
(参考情報)
judge5_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'std::pair<std::vector<int>, std::vector<int> > {anonymous}::primes_lpf(int)':
main.cpp:32:48: warning: result of '2^4' is 6; did you mean '1 << 4' (16)? [-Wxor-used-as-pow]
   32 |   for (; x <= n5; x += add_next, add_next ^= 2 ^ 4) {
      |                                                ^
      |                                              -
      |                                              1 <<
main.cpp:32:46: note: you can silence this warning by using a hexadecimal constant (0x2 rather than 2)
   32 |   for (; x <= n5; x += add_next, add_next ^= 2 ^ 4) {
      |                                              ^
      |                                              0x2
main.cpp:46:47: warning: result of '2^4' is 6; did you mean '1 << 4' (16)? [-Wxor-used-as-pow]
   46 |   for (; x <= n; x += add_next, add_next ^= 2 ^ 4) {
      |                                               ^
      |                                             -
      |                                             1 <<
main.cpp:46:45: note: you can silence this warning by using a hexadecimal constant (0x2 rather than 2)
   46 |   for (; x <= n; x += add_next, add_next ^= 2 ^ 4) {
      |                                             ^
      |                                             0x2

ソースコード

diff #
raw source code

#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