public void solve(int testNumber, InputReader in, OutputWriter out) { int length = in.readInt(); char[] startState = in.readString().toCharArray(); int startPosition = in.readInt() - 1; char[] endState = in.readString().toCharArray(); int endPosition = in.readInt() - 1; out.printLine("M1"); for (int i = 0; i < 10; i++) out.printLine("<"); out.printLine("E"); for (int i = 0; i < 27; i++) { int first = i % 3; int second = i / 3 % 3; int third = i / 9; out.printLine("M" + (100 + i)); if (first == 1) out.printLine("v"); else if (first == 2) out.printLine("x"); out.printLine(">"); if (second == 1) out.printLine("v"); else if (second == 2) out.printLine("x"); out.printLine(">"); if (third == 1) out.printLine("v"); else if (third == 2) out.printLine("x"); out.printLine(">"); out.printLine("E"); } int position = startPosition; while (position >= 10) { out.printLine("#1"); position -= 10; } while (position > 0) { out.printLine("<"); position--; } for (int i = 0; i + 3 < length; i += 3) { int code = 0; for (int j = 2; j >= 0; j--) { code *= 3; if (startState[j + i] == '.' && endState[j + i] == '-') code++; else if (startState[j + i] == '-' && endState[j + i] == '.') code += 2; } out.printLine("#" + (100 + code)); } int end = (length - 1) - ((length - 1) % 3); for (int i = end; i < length; i++) { if (startState[i] == '.' && endState[i] == '-') out.printLine("v"); else if (startState[i] == '-' && endState[i] == '.') out.printLine("x"); if (i != length - 1) out.printLine(">"); } position = length - 1; while (position - endPosition >= 10) { out.printLine("#1"); position -= 10; } while (position > endPosition) { out.printLine("<"); position--; } out.printLine("s"); }
public void solve(int testNumber, InputReader in, OutputWriter out) { int firstCount = in.readInt(); int[] first = IOUtils.readIntArray(in, firstCount); int secondCount = in.readInt(); int[] second = IOUtils.readIntArray(in, secondCount); int[] countFirst = countDivisors(first); int[] countSecond = countDivisors(second); Arrays.sort(first); Arrays.sort(second); long result = 1; double doubleResult = 1; for (int i = 0; i < primes.length; i++) { for (int j = 0; j < Math.min(countFirst[i], countSecond[i]); j++) { result *= primes[i]; result %= MOD; doubleResult *= primes[i]; } } int firstIndex = 0; int secondIndex = 0; while (firstIndex < firstCount && secondIndex < secondCount) { if (first[firstIndex] == second[secondIndex]) { result *= first[firstIndex]; result %= MOD; doubleResult *= first[firstIndex]; firstIndex++; secondIndex++; } else if (first[firstIndex] < second[secondIndex]) firstIndex++; else secondIndex++; } if (result == doubleResult) out.printLine(result); else out.printFormat("%09d\n", (int) result); }
public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); int[] computer = IOUtils.readIntArray(in, count); int[][] required = new int[count][]; for (int i = 0; i < count; i++) { int requiredCount = in.readInt(); required[i] = IOUtils.readIntArray(in, requiredCount); } MiscUtils.decreaseByOne(required); int answer = Integer.MAX_VALUE; for (int i = 1; i <= 3; i++) answer = Math.min(answer, go(computer, required, i)); out.printLine(answer); }
public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); int surprising = in.readInt(); int required = in.readInt(); int[] scores = IOUtils.readIntArray(in, count); int answer = 0; for (int i : scores) { if (i < required) continue; if (i >= 3 * required - 2) answer++; else if (i >= 3 * required - 4 && surprising > 0) { answer++; surprising--; } } out.printLine("Case #" + testNumber + ":", answer); }
public void solve(int testNumber, InputReader in, PrintWriter out) { int from = in.readInt(); int to = in.readInt(); long[] happy = new long[(1 << 11) - 1]; happy[1] = 4; happy[2] = 7; int first = 1; int last = 3; for (int i = 2; i <= 10; i++) { for (int j = 0; j < last - first; j++) { happy[last + 2 * j] = 10 * happy[first + j] + 4; happy[last + 2 * j + 1] = 10 * happy[first + j] + 7; } int next = last + 2 * (last - first); first = last; last = next; } long answer = 0; for (int i = 1; i < happy.length; i++) answer += Math.max(0, Math.min(happy[i], to) - Math.max(from - 1, happy[i - 1])) * happy[i]; out.println(answer); }
public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); int queryCount = in.readInt(); LongIntervalTree tree = new LongIntervalTree(count) { @Override protected long joinValue(long left, long right) { return left + right; } @Override protected long joinDelta(long was, long delta) { return was ^ delta; } @Override protected long accumulate(long value, long delta, int length) { if (delta == 0) return value; return length - value; } @Override protected long neutralValue() { return 0; } @Override protected long neutralDelta() { return 0; } }; for (int i = 0; i < queryCount; i++) { int type = in.readInt(); int from = in.readInt() - 1; int to = in.readInt() - 1; if (type == 0) tree.update(from, to, 1); else out.printLine(tree.query(from, to)); } }
public void solve(int testNumber, InputReader in, OutputWriter out) { count = in.readInt(); from = new int[count]; to = new int[count]; IOUtils.readIntArrays(in, from, to); previousSame = new int[count]; for (int i = 0; i < count; i++) { for (int j = 0; j < i; j++) { if (from[i] == from[j] && to[i] == to[j] || from[i] == to[j] && to[i] == from[j]) previousSame[i] += 1 << j; } } result = new long[8][8][1 << count]; ArrayUtils.fill(result, -1); long answer = 0; for (int i = 0; i < count; i++) { if (previousSame[i] == 0) { answer += go(from[i], to[i], (1 << count) - 1 - (1 << i)); if (from[i] != to[i]) answer += go(to[i], from[i], (1 << count) - 1 - (1 << i)); } } out.printLine(answer); }
public void solve(int testNumber, InputReader in, PrintWriter out) { int stringIndex = in.readInt(); long letterIndex = in.readLong(); out.println(go(stringIndex, letterIndex)); }
public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int m = in.readInt(); int d = in.readInt(); int l = in.readInt(); int[] x = new int[n]; for (int i = 1; i < n; ++i) x[i] = in.readInt(); int[] z = new int[m]; for (int i = 0; i < m; ++i) z[i] = in.readInt(); int[] p = new int[d]; int[] r = new int[d]; for (int i = 0; i < d; ++i) { p[i] = in.readInt(); r[i] = in.readInt(); } int[] interestingCoord = new int[2 * d + 2 * m + n]; int pos = 0; for (int i = 0; i < n; ++i) { interestingCoord[pos++] = x[i]; } for (int i = 0; i < m; ++i) { interestingCoord[pos++] = z[i] - l; interestingCoord[pos++] = z[i] + l; } for (int i = 0; i < d; ++i) { interestingCoord[pos++] = r[i] - l; interestingCoord[pos++] = r[i] + l; } if (pos != interestingCoord.length) throw new RuntimeException(); Arrays.sort(interestingCoord); pos = 0; for (int i = 0; i < interestingCoord.length; ++i) { if (i == 0 || interestingCoord[i] != interestingCoord[i - 1]) interestingCoord[pos++] = interestingCoord[i]; } int[] cntGopher = new int[pos]; boolean[] haveCD = new boolean[pos]; int[] nextCD = new int[pos + 1]; int[] prevCD = new int[pos + 1]; nextCD[pos] = pos; prevCD[pos] = pos; int[] cntCD = new int[pos]; for (int i = 0; i < n; ++i) { update(cntGopher, decodePos(interestingCoord, pos, x[i]), 1); } int res = 0; for (int i = 0; i < m; ++i) { int leftPos = decodePos(interestingCoord, pos, z[i] - l); int rightPos = decodePos(interestingCoord, pos, z[i] + l); int prevCDPos = getLast(cntCD, haveCD, rightPos); if (prevCDPos == -1) prevCDPos = pos; int nextCDPos = nextCD[prevCDPos]; int finalLeft = leftPos; int finalRight = rightPos; if (prevCDPos != pos) finalLeft = Math.max(finalLeft, prevCDPos + 1); if (nextCDPos != pos) { int nextCDLeft = decodePos(interestingCoord, pos, interestingCoord[nextCDPos] - 2 * l); finalRight = Math.min(finalRight, nextCDLeft - 1); } if (finalLeft <= finalRight) { res += get(cntGopher, finalRight) - get(cntGopher, finalLeft - 1); } nextCD[rightPos] = nextCDPos; prevCD[rightPos] = prevCDPos; nextCD[prevCDPos] = rightPos; prevCD[nextCDPos] = rightPos; if (haveCD[rightPos]) throw new RuntimeException(); haveCD[rightPos] = true; update(cntCD, rightPos, 1); } out.printLine(res); for (int i = 0; i < d; ++i) { { int leftPos = decodePos(interestingCoord, pos, p[i] - l); int rightPos = decodePos(interestingCoord, pos, p[i] + l); if (!haveCD[rightPos]) throw new RuntimeException(); int prevCDPos = prevCD[rightPos]; int nextCDPos = nextCD[rightPos]; int finalLeft = leftPos; int finalRight = rightPos; if (prevCDPos != pos) finalLeft = Math.max(finalLeft, prevCDPos + 1); if (nextCDPos != pos) { int nextCDLeft = decodePos(interestingCoord, pos, interestingCoord[nextCDPos] - 2 * l); finalRight = Math.min(finalRight, nextCDLeft - 1); } if (finalLeft <= finalRight) { res -= get(cntGopher, finalRight) - get(cntGopher, finalLeft - 1); } nextCD[prevCDPos] = nextCDPos; prevCD[nextCDPos] = prevCDPos; haveCD[rightPos] = false; update(cntCD, rightPos, -1); } { int leftPos = decodePos(interestingCoord, pos, r[i] - l); int rightPos = decodePos(interestingCoord, pos, r[i] + l); int prevCDPos = getLast(cntCD, haveCD, rightPos); if (prevCDPos == -1) prevCDPos = pos; int nextCDPos = nextCD[prevCDPos]; int finalLeft = leftPos; int finalRight = rightPos; if (prevCDPos != pos) finalLeft = Math.max(finalLeft, prevCDPos + 1); if (nextCDPos != pos) { int nextCDLeft = decodePos(interestingCoord, pos, interestingCoord[nextCDPos] - 2 * l); finalRight = Math.min(finalRight, nextCDLeft - 1); } if (finalLeft <= finalRight) { res += get(cntGopher, finalRight) - get(cntGopher, finalLeft - 1); } nextCD[rightPos] = nextCDPos; prevCD[rightPos] = prevCDPos; nextCD[prevCDPos] = rightPos; prevCD[nextCDPos] = rightPos; if (haveCD[rightPos]) throw new RuntimeException(); haveCD[rightPos] = true; update(cntCD, rightPos, 1); } out.printLine(res); } }
public void solve(int testNumber, InputReader in, OutputWriter out) { out.printLine(answer[in.readInt()]); }