結果

問題 No.2496 LCM between Permutations
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-10-06 22:34:22
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 4,044 bytes
コンパイル時間 1,918 ms
コンパイル使用メモリ 153,772 KB
実行使用メモリ 24,396 KB
平均クエリ数 953.86
最終ジャッジ日時 2023-10-06 22:34:28
合計ジャッジ時間 5,202 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
23,424 KB
testcase_01 AC 23 ms
23,664 KB
testcase_02 AC 23 ms
24,036 KB
testcase_03 AC 23 ms
24,036 KB
testcase_04 AC 24 ms
23,556 KB
testcase_05 AC 24 ms
23,556 KB
testcase_06 AC 23 ms
24,396 KB
testcase_07 AC 24 ms
23,388 KB
testcase_08 AC 24 ms
23,652 KB
testcase_09 AC 23 ms
24,060 KB
testcase_10 AC 24 ms
23,400 KB
testcase_11 AC 25 ms
23,436 KB
testcase_12 AC 24 ms
23,556 KB
testcase_13 AC 25 ms
24,396 KB
testcase_14 AC 24 ms
24,276 KB
testcase_15 AC 31 ms
23,568 KB
testcase_16 AC 33 ms
24,036 KB
testcase_17 AC 36 ms
23,868 KB
testcase_18 AC 99 ms
24,048 KB
testcase_19 AC 75 ms
23,568 KB
testcase_20 AC 103 ms
23,400 KB
testcase_21 AC 90 ms
24,036 KB
testcase_22 AC 85 ms
24,048 KB
testcase_23 AC 116 ms
24,384 KB
testcase_24 AC 98 ms
23,436 KB
testcase_25 AC 125 ms
23,700 KB
testcase_26 AC 126 ms
23,400 KB
testcase_27 AC 125 ms
24,048 KB
testcase_28 AC 127 ms
24,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

uint xrand() {
  static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}


int N;
debug {
  int[] A, B;
}

int counter;
int Ask(int i, int j) {
  ++counter;
  if (counter > 3 * N) {
    assert(false);
  }
  debug {
    const l = lcm(A[i], B[j]);
    if (N <= 10) {
      writeln(i, " ", j, ": ", A[i], " ", B[j], "; ", l);
    }
    return l;
  } else {
    writefln("? %s %s", i + 1, j + 1);
    stdout.flush;
    const l = readInt;
    return l;
  }
}
void Answer(int[] as, int[] bs) {
  write("!");
  foreach (i; 0 .. N) write(" ", as[i]);
  foreach (j; 0 .. N) write(" ", bs[j]);
  writeln;
  stdout.flush;
  debug {
    assert(A == as);
    assert(B == bs);
  }
  import core.stdc.stdlib;
  exit(0);
}

void main() {
  N = readInt;
  debug {
    A = iota(1, N + 1).array;
    B = iota(1, N + 1).array;
    xrand;
    foreach (i; 0 .. N) swap(A[xrand() % (i + 1)], A[i]);
    foreach (i; 0 .. N) swap(B[xrand() % (i + 1)], B[i]);
    writeln("A = ", A);
    writeln("B = ", B);
  }
  
  auto lpf = iota(N + 1).array;
  int P;
  for (int p = 2; p <= N; ++p) if (lpf[p] == p) {
    chmax(P, p);
    for (int n = 2 * p; n <= N; n += p) chmin(lpf[n], p);
  }
  
  auto as = new int[N];
  auto bs = new int[N];
  void checkA(int jm) {
    int rem;
    foreach (i; 0 .. N - 1) {
      const res = Ask(i, jm);
      if (res == P) {
        if (rem) {
          as[i] = rem;
        } else {
          const res1 = Ask(i, (jm + 1) % N);
          if (res1 % P == 0) {
            as[i] = P;
            rem = 1;
          } else {
            as[i] = 1;
            rem = P;
          }
        }
      } else {
        as[i] = res / P;
      }
    }
    as[N - 1] = N * (N + 1) / 2 - as[0 .. N - 1].sum;
  }
  void checkB(int im) {
    int rem;
    foreach (j; 0 .. N - 1) {
      const res = Ask(im, j);
      if (res == P) {
        if (rem) {
          bs[j] = rem;
        } else {
          const res1 = Ask((im + 1) % N, j);
          if (res1 % P == 0) {
            bs[j] = P;
            rem = 1;
          } else {
            bs[j] = 1;
            rem = P;
          }
        }
      } else {
        bs[j] = res / P;
      }
    }
    bs[N - 1] = N * (N + 1) / 2 - bs[0 .. N - 1].sum;
  }
  
  if (N == 1) {
    as[0] = 1;
    bs[0] = 1;
  } else {
    auto cs = new int[N];
    foreach (j; 0 .. N) cs[j] = Ask(0, j);
    int jm = -1;
    foreach (j; 0 .. N) if (cs[j] % P == 0) {
      if (jm != -1) jm = -2;
      if (jm == -1) jm = j;
    }
    if (jm == -2) {
      // A[0] = p
      checkB(0);
      jm = -1;
      foreach (j; 0 .. N) if (bs[j] == P) jm = j;
      checkA(jm);
    } else {
      // B[jm] = p
      checkA(jm);
      int im = -1;
      foreach (i; 0 .. N) if (as[i] == P) im = i;
      checkB(im);
    }
  }
  Answer(as, bs);
}
0