/* ACM Mid-Central Programming competition 2013 Problem D solution by Andy Harrington This file, paradoxGauss.java, has the same documentation as for paradox.java, (see that file) except that paradox.java uses int determinants for linear equation solutions, while this code does Gauss elimination with exact integer arithmetic. Only the last step brings in a rational number. The C++ solution solves a linear system by substitution, removing one variable at a time, with exact rational arithmetic. If you alter the code near the bottom to set debug to true, you can see all the initial matrices and the final row reduced ones, and the largest integer used in calculation. */ import java.util.*; import java.io.*; import static java.lang.Math.*; public class paradoxGauss // version with Gauss elimination in integers { static char H = 'H', T = 'T'; // two symbols static char[] HT = {H, T}; static String a, b, // given patterns in the input C = "C"; // search key for constant side of equation static List pre, // all prefixes in a, b aug; // for augmented matrix with keys pre + C static int n, // a, b length pn; // pre length public static void main(String[] args) throws Exception { String file = (args.length > 0) ? args[0] : "paradox.in"; Scanner in = new Scanner(new File(file)); a = in.next(); while (!a.equals("$")) { b = in.next(); n = a.length(); pre = new ArrayList(); addPre(a); addPre(b); pn = pre.size(); aug = new ArrayList(pre); aug.add(C); // for augmented matrix: C for Constant long[][] m = makeSys(); System.out.println(solve1(m)); a = in.next(); } judgeWrapup(); // judge } static void addPre(String s) { // Add to pre all new prefixes of s for (int i=0; i