結果

問題 No.1711 Divide LCM
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-10-27 20:58:28
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 565 ms / 4,000 ms
コード長 3,869 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 108,964 KB
実行使用メモリ 65,444 KB
最終ジャッジ日時 2023-09-04 18:43:04
合計ジャッジ時間 15,769 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 11 ms
4,384 KB
testcase_05 AC 36 ms
4,384 KB
testcase_06 AC 12 ms
4,384 KB
testcase_07 AC 30 ms
4,380 KB
testcase_08 AC 15 ms
4,384 KB
testcase_09 AC 64 ms
12,088 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 88 ms
13,668 KB
testcase_12 AC 14 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 6 ms
4,384 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 3 ms
4,384 KB
testcase_17 AC 6 ms
4,384 KB
testcase_18 AC 218 ms
22,216 KB
testcase_19 AC 218 ms
21,668 KB
testcase_20 AC 217 ms
22,172 KB
testcase_21 AC 302 ms
27,664 KB
testcase_22 AC 565 ms
63,360 KB
testcase_23 AC 518 ms
65,444 KB
testcase_24 AC 238 ms
17,788 KB
testcase_25 AC 235 ms
17,624 KB
testcase_26 AC 229 ms
22,308 KB
testcase_27 AC 242 ms
16,672 KB
testcase_28 AC 230 ms
16,732 KB
testcase_29 AC 5 ms
4,384 KB
testcase_30 AC 249 ms
17,592 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 310 ms
26,648 KB
testcase_33 AC 303 ms
26,668 KB
testcase_34 AC 12 ms
4,380 KB
testcase_35 AC 236 ms
16,964 KB
testcase_36 AC 241 ms
17,040 KB
testcase_37 AC 242 ms
16,920 KB
testcase_38 AC 257 ms
23,356 KB
testcase_39 AC 308 ms
22,644 KB
testcase_40 AC 5 ms
4,380 KB
testcase_41 AC 5 ms
4,380 KB
testcase_42 AC 5 ms
4,380 KB
testcase_43 AC 6 ms
4,384 KB
testcase_44 AC 6 ms
4,380 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; }
real readReal() { return readToken.to!real; }

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)); }




void main() {
  try {
    for (; ; ) {
      const N = readInt;
      auto M = new int[N];
      auto P = new int[][N];
      auto E = new int[][N];
      auto A = new int[N];
      A[] = 1;
      foreach (i; 0 .. N) {
        M[i] = readInt;
        P[i] = new int[M[i]];
        E[i] = new int[M[i]];
        foreach (j; 0 .. M[i]) {
          P[i][j] = readInt;
          E[i][j] = readInt;
          A[i] *= P[i][j]^^E[i][j];
        }
      }
      
      int maxP;
      foreach (i; 0 .. N) {
        chmax(maxP, P[i].maxElement);
      }
      auto maxEs = new int[maxP + 1];
      foreach (i; 0 .. N) {
        foreach (j; 0 .. M[i]) {
          chmax(maxEs[P[i][j]], E[i][j]);
        }
      }
      int[] qs;
      foreach (q; 0 .. maxP + 1) if (maxEs[q]) {
        qs ~= q;
      }
      const qsLen = cast(int)(qs.length);
      debug {
        writeln("qs = ", qs);
      }
      
      bool bad;
      bool[long] app;
      auto prods = new int[1 << 9];
      auto bs = new int[N];
      foreach (i; 0 .. N) {
        int[] ps;
        foreach (j; 0 .. M[i]) if (maxEs[P[i][j]] == E[i][j]) {
          ps ~= P[i][j];
        }
        const psLen = cast(int)(ps.length);
        if (psLen == qsLen) {
          bad = true;
          break;
        }
        assert(psLen <= 9);
        prods[0] = 1;
        foreach (j; 0 .. psLen) foreach (h; 0 .. 1 << j) {
          prods[h | 1 << j] = prods[h] * ps[j];
        }
        foreach (h; 0 .. 1 << psLen) {
          app[prods[h]] = true;
        }
        bs[i] = prods[(1 << psLen) - 1];
      }
      if (bad) {
        writeln(-1);
        continue;
      }
      debug {
        writeln("app = ", app);
        writeln("bs = ", bs);
      }
      
      int best = 10;
      int[] ans;
      int[] chosen = new int[10];
      void dfs(int pos, int last, long prod) {
        if (best <= pos) {
          return;
        }
        if (prod !in app) {
          best = pos;
          ans = chosen.dup;
          return;
        }
        foreach (k; last + 1 .. qsLen) {
          chosen[pos] = qs[k];
          dfs(pos + 1, k, prod * qs[k]);
          if (best <= pos + 1) {
            return;
          }
        }
      }
      dfs(0, -1, 1);
      ans.length = best;
      debug {
        writeln("ans = ", ans);
      }
      
      auto used = new bool[N];
      writeln(best);
      foreach (q; ans) {
        int cnt;
        foreach (i; 0 .. N) if (!used[i] && bs[i] % q != 0) {
          ++cnt;
        }
        write(cnt);
        foreach (i; 0 .. N) if (!used[i] && bs[i] % q != 0) {
          used[i] = true;
          write(" ", A[i]);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}
0