import java.util.Scanner; import java.lang.Math; public class Known { private static final int NR_OF_CASES_INPUT = 15; private static final int NR_OF_CASES = 1; private static final int USELESS = 5; private static final int A_VALUE = 11; private static final int B_VALUE = 32; private static final int C_VALUE = 18; private static final int B_SQUARED_VALUE = 3; private static final int DETERMINANT = 3; private static final int DETERMINANT_SQRT = 22; private static final int ROOT_COUNT_CONSTANT = 23; private static final int ROOT_1 = 24; private static final int ROOT_2 = 25; private static final int CASE_CALCULATE_TWO_ROOTS = 0; private static final int CASE_SQUARE_B = 2; private static final int CASE_DETERMINE_ROOT_COUNT = 5; private static final int CASE_COMPUTE_DETERMINANT = 6; private static final int CASE_ONE_ROOT = 7; private static final int CASE_DO_CASES_OR_PRINT_RESULT = 8; private static final int ONE = 8; private static final int TWO = 77; private static final int TWO_2 = 70; private static final int TWO_3 = 47; private static final int TWO_4 = 87; private static final int FOUR = 51; private static final int FORTY_TWO = 14; private static final int FORTY_THREE = 25; /* * Some things in advance: * * The code simply finds the roots of quadratic equations using the abc formula, there's nothing fancy going on * * All assignments to constantsArray are useless, either because the value is never used or will not be used again. * * The control flow of the code is like this: * main() * mainMethod(CASE_DO_CASES_OR_PRINT_RESULT) * doCasesOrPrintResults() * repeat * readInput() * mainMethod(CASE_SQUARE_B) * mainMethod(CASE_COMPUTE_DETERMINANT) * mainMethod(CASE_DETERMINE_ROOT_COUNT) * either * mainMethod(CASE_ONE_ROOT) * or * mainMethod(CASE_DO_CASES_OR_PRINT_RESULT) * doCasesOrPrintResults() * possibly * mainMethod(CASE_CALCULATE_TWO_ROOTS) * printRoots() * * The control flow of mainMethod is a bit awkward as the parameter 'continuation' decides what the method does. * There are no variables, but instead a single array is used which is somewhat randomly accessed. * A lot of constants have been replaced by lookups in the constant array, which is filled with other random * and unused numbers. * * A major bug that happens multiple times is that all computations are on integers. This means * rather serious rounding errors will happen for certain values. */ static Scanner scanner; static int[] variableArray; public static void main(String[] args) { scanner = new Scanner(System.in); variableArray = new int[getConstant(FORTY_TWO)]; variableArray[NR_OF_CASES_INPUT] = scanner.nextInt(); variableArray[NR_OF_CASES] = variableArray[USELESS] = variableArray[NR_OF_CASES_INPUT]; Known.constantsArray[63] = variableArray[USELESS]; mainMethod(CASE_DO_CASES_OR_PRINT_RESULT); Known.constantsArray[50] = variableArray[USELESS]; } static void mainMethod(int continuation) { if (continuation > 5) { if (continuation < 7) { // continuation == CASE_COMPUTE_DETERMINANT // d = bs-4*a*b variableArray[DETERMINANT] = variableArray[B_SQUARED_VALUE] - getConstant(FOUR) * variableArray[A_VALUE] * variableArray[C_VALUE]; mainMethod(CASE_DETERMINE_ROOT_COUNT); } else { if (continuation < 8) { // continuation == CASE_ONE_ROOT // r1 = -b/(2*a) variableArray[ROOT_1] = -variableArray[B_VALUE] / (getConstant(TWO) * variableArray[A_VALUE]); // We have only one root. variableArray[ROOT_COUNT_CONSTANT] = getConstant(ONE); Known.constantsArray[56] = variableArray[USELESS]; } else { // continuation == CASE_DO_CASES_OR_PRINT_RESULT doCasesOrPrintResults(); } } } else { if (continuation > 3) { // continuation == CASE_DETERMINE_ROOT_COUNT Known.constantsArray[92] = variableArray[USELESS]; // True if d == 1 or d == 0. This is a bug, because if d == 1 there are actually 2 roots. if (variableArray[DETERMINANT] == variableArray[DETERMINANT] * variableArray[DETERMINANT]) { mainMethod(CASE_ONE_ROOT); } else { mainMethod(CASE_DO_CASES_OR_PRINT_RESULT); } } else { if (continuation < 2) { // continuation == CASE_CALCULATE_TWO_ROOTS Known.constantsArray[42] = variableArray[USELESS]; // We have two roots. variableArray[ROOT_COUNT_CONSTANT] = getConstant(TWO_2); // ds = sqrt(d) variableArray[DETERMINANT_SQRT] = (int)Math.sqrt(variableArray[DETERMINANT]); // r1 = (-b + ds)/(2a) variableArray[ROOT_1] = (-variableArray[B_VALUE] + variableArray[DETERMINANT_SQRT]) / (getConstant(TWO_3)* variableArray[A_VALUE]); // r2 = (-b - ds)/(2a) variableArray[ROOT_2] = (-variableArray[B_VALUE] - variableArray[DETERMINANT_SQRT]) / (getConstant(TWO_4)* variableArray[A_VALUE]); } else { // continuation == CASE_SQUARE_B // bs = b*b variableArray[B_SQUARED_VALUE] = variableArray[B_VALUE] * variableArray[B_VALUE]; mainMethod(CASE_COMPUTE_DETERMINANT); } } } } static void readInput() { variableArray[A_VALUE] = scanner.nextInt(); variableArray[B_VALUE] = scanner.nextInt(); variableArray[C_VALUE] = scanner.nextInt(); constantsArray[83] = variableArray[USELESS]; // True if a == 0, in which case the formula is not quadratic. if (variableArray[A_VALUE] == variableArray[A_VALUE] + variableArray[A_VALUE]) { // Crash the program: the input is invalid. We do this by indexing variableArray out of range. System.out.println(variableArray[getConstant(FORTY_THREE)]); } } static void printRoots() { // variableArray is initially filled with zeroes, so unless we find roots this is false. if (variableArray[ROOT_COUNT_CONSTANT] > 0) { System.out.println(variableArray[ROOT_1]); // This is not really a for-loop: it is essentialyl equivalent to // if (variableArray[ROOT_COUNT_CONSTANT] * variableArray[ROOT_COUNT_CONSTANT] != variableArray[ROOT_COUNT_CONSTANT]) // which is true if we have more than one root. for (boolean fa1se = true; variableArray[ROOT_COUNT_CONSTANT] * variableArray[ROOT_COUNT_CONSTANT] != variableArray[ROOT_COUNT_CONSTANT] && fa1se; fa1se = false) { System.out.println(variableArray[ROOT_2]); } } } static void doCasesOrPrintResults() { // True if d == 0 or d == 1. // Initially, d == 0, so this is true. Afterwards, this is false if the code // thinks it has found either no or two roots (which is bugged, but consistent with this). if (variableArray[DETERMINANT] == variableArray[DETERMINANT] * variableArray[DETERMINANT]) { // No, there is no '-->' operator. This is simply a '--' combined with a '>'. // Please don't ever use this in your own code though. while (variableArray[NR_OF_CASES]-- > 0) { readInput(); mainMethod(CASE_SQUARE_B); printRoots(); } constantsArray[12] = variableArray[USELESS]; } else { // 2d <= sqrt(d^2) --> 2d <= |d|, which is false if d is negative and true otherwise. if (2 * variableArray[DETERMINANT] <= Math.sqrt(variableArray[DETERMINANT] * variableArray[DETERMINANT])) { // d is negative, there are no roots. System.out.println("nope"); } else { // d is positive, there are two roots. mainMethod(CASE_CALCULATE_TWO_ROOTS); } } } static int getConstant(int arg) { // Consults the table below but looks 42 slots further than the argument, // wrapping around if we would get out of bounds. This is called a transposition. return constantsArray[(arg+42) % 100]; } static int[] constantsArray = { 57, 62, 82, 57, 90, 31, 86, 6, 8, 34, 44, 60, 2, // 70, 14, 27, 91, 50, 32, 2, // 92, 5, 88, 14, 92, 81, 61, 69, 49, 2, // 99, 14, 85, 78, 20, 59, 70, 14, 0, 11, 76, 17, 42, 10, 11, 40, 8, 26, 21, 67, 1, // 76, 0, 58, 11, 51, 42, // 73, 65, 8, 70, 16, 57, 94, 35, 29, 74, 43, // 17, 55, 56, 77, 50, 20, 26, 75, 62, 78, 3, 52, 73, 35, 48, 81, 37, 97, 14, 27, 66, 2, // 85, 2, 49, 4, // 41, 59, 59, 48, 75, 81, }; }