package chelper; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import io.InputReader; import io.OutputWriter; import misc.SimpleSavingChelperSolution; public class Brute extends SimpleSavingChelperSolution { private int condCount; private List>> X; private List> multipliers; private List consts; private long[] values; private boolean[] set; private int[] termCounts; private int[] bruteOrder; public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } long minValue = 33; long maxValue = 126; @Override public void solve(int testNumber) { condCount = in.nextInt(); X = new ArrayList<>(); multipliers = new ArrayList<>(); consts = new ArrayList<>(); set = new boolean[condCount]; values = new long[condCount]; termCounts = new int[condCount]; for (int i = 0; i < condCount; i++) { X.add(new ArrayList<>()); multipliers.add(new ArrayList<>()); termCounts[i] = in.nextInt(); for (int j = 0; j < termCounts[i]; j++) { int multCount = in.nextInt(); X.get(i).add(new ArrayList<>()); for (int k = 0; k < multCount; k++) { int mult = in.nextInt(); X.get(i).get(j).add(mult); } } for (int j = 0; j < termCounts[i]; j++) { multipliers.get(i).add(in.nextLong()); } consts.add(in.nextLong()); } bruteOrder = in.nextIntArray(condCount); System.out.println("test"); for (int i = 0; i < condCount; i++) { System.out.printf( "%12d %12d %12d\n", estimate(i, false), consts.get(i), estimate(i, true)); } System.out.println(okEstimates()); brute(0); } long estimate(int cond, boolean max) { long res = 0; for (int i = 0; i < termCounts[cond]; i++) { long term = 1; long m = multipliers.get(cond).get(i); boolean localMax = max == m > 0; for (int x : X.get(cond).get(i)) { if (set[x]) { term *= values[x]; } else { if (localMax) { term *= maxValue; } else { term *= minValue; } } } res += term * m; } return res; } boolean okEstimates() { for (int cond = 0; cond < condCount; cond++) { long min = estimate(cond, false); long max = estimate(cond, true); if (min > consts.get(cond) || max < consts.get(cond)) { // System.out.println("bad " + cond); return false; } } return true; } long iters = 0; void brute(int depth) { if (depth >= condCount) { System.out.println("Found " + Arrays.toString(values)); System.out.flush(); return; } int var = bruteOrder[depth]; for (long value = minValue; value <= maxValue; value++) { values[var] = value; set[var] = true; if (okEstimates()) { iters++; if (iters % 10000 == 0) { System.out.printf("iter = %10d\n", iters); System.out.printf("depth = %5d\n", depth); System.out.print("i = "); for (int i : bruteOrder) { System.out.printf("%3d ", i); } System.out.println(); System.out.print("a = "); for (int i : bruteOrder) { System.out.printf("%3d ", values[i]); } System.out.println(); System.out.flush(); } brute(depth + 1); } set[var] = false; } } }