結果
| 問題 |
No.1711 Divide LCM
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-27 20:58:28 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 452 ms / 4,000 ms |
| コード長 | 3,869 bytes |
| コンパイル時間 | 918 ms |
| コンパイル使用メモリ | 123,176 KB |
| 実行使用メモリ | 66,536 KB |
| 最終ジャッジ日時 | 2024-06-22 16:30:45 |
| 合計ジャッジ時間 | 12,034 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
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) {
}
}