Commits (4)
 import lib.BooleanUtils; class Test { public static void main(String[] args) { BooleanUtils bu = new BooleanUtils(); boolean b1 = 0 == 0; boolean b2 = 0 != 0; boolean b3 = !true; boolean b4 = !!!true == true; bu.println(b1); bu.println(b2); bu.println(b3); bu.println(b4); } } \ No newline at end of file
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file
 /** * Linear congruence generator for pseudo random numbers. */ class LCG { public int a; public int c; public int m; public int val; public LCG initWithDefault(int seed){ return init(2147483629, 2147483587, 2147483647, seed); } public LCG initWithDefault2(int seed){ return init(2147480707, 2147480707, 2147482367, seed); } public LCG init(int a_, int c_, int m_, int startVal) { a = a_; c = c_; m = m_; val = startVal; return this; } public int nextVal() { return val = (a * val + c) % m; } /* util functions */ public int abs(int v) { if (v >= 0) return v; return -v; } public void runTest() { int i = 0; while (i < 100) { i = i + 1; System.out.println(abs(nextVal())); } /*System.out.println(-2147483648 / -1);*/ } /** * @param min minimum number * @param max exclusive range end */ public int nextRange(int min, int max){ return nextVal() % (max - min) + min; } } /* Adapted from: https://en.wikipedia.org/wiki/Lehmer_random_number_generator */ class LehmerRandom { public int M; /* 2^31 - 1 (A large prime number) */ public int A; /* Prime root of M, passes statistical tests and produces a full cycle */ public int Q; /* M / A (To avoid overflow on A * seed) */ public int R; /* M % A (To avoid overflow on A * seed) */ public int seed; public LehmerRandom init(int seed){ this.M = 2147483647; this.A = 16807; this.Q = 127773; this.R = 2836; this.seed = seed; return this; } public LehmerRandom initWithDefault(){ return init(2147480677); } public int random() { int hi = seed / Q; int lo = seed % Q; int test = A * lo - R * hi; if (test <= 0) test = test + M; seed = test; return test; } public int next(){ return random(); } /** * @param min minimum number * @param max exclusive range end */ public int nextRange(int min, int max){ return next() % (max - min) + min; } public int[] intArray(int size, int min, int maxEx){ int[] arr = new int[size]; int i = 0; while (i < size){ arr[i] = nextRange(min, maxEx); i = i + 1; } return arr; } public boolean nextBoolean(){ return next() % 2 == 0; } public boolean[] booleanArray(int size){ boolean[] arr = new boolean[size]; int i = 0; while (i < size){ arr[i] = nextBoolean(); i = i + 1; } return arr; } public void shuffleIntArray(int[] arr, int size) { int i = size - 1; while (i > 0){ int index = nextRange(0, i + 1); int a = arr[index]; arr[index] = arr[i]; arr[i] = a; i = i - 1; } } } /** * Calculate the tensor product for a dimension of 7 */ class BigTensorProduct { public static void main(String[] args) throws Exception { BigTensorProduct prod = new BigTensorProduct(); prod.run(15, false, System.in.read()); } public void run(int n, boolean outputMatrix, int seed){ LCG random = new LCG().initWithDefault2(seed); int sum = 0; int[] indezes = new int[7]; int j = 0; while (j < 7){ indezes[j] = 0; j = j + 1; } int[][][][][][][] arr = randomIntArray(n, seed + 1); int[][] vectors = randomMatrix(7, n, seed + 2); while (indezes[0] < n) { while (indezes[1] < n) { while (indezes[2] < n) { while (indezes[3] < n) { while (indezes[4] < n) { while (indezes[5] < n) { while (indezes[6] < n) { int val = 1; int i = 0; while (i < 7){ val = val * vectors[i][indezes[i]]; i = i + 1; } if (outputMatrix){ System.out.println(arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]]); } sum = sum + val * arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]]; indezes[6] = indezes[6] + 1; } indezes[6] = 0; indezes[5] = indezes[5] + 1; } indezes[5] = 0; indezes[4] = indezes[4] + 1; } indezes[4] = 0; indezes[3] = indezes[3] + 1; } indezes[3] = 0; indezes[2] = indezes[2] + 1; } indezes[2] = 0; indezes[1] = indezes[1] + 1; } indezes[1] = 0; indezes[0] = indezes[0] + 1; } System.out.println(sum); } public void runWithNumbers(int n, boolean outputMatrix, int seed){ LCG random = new LCG().initWithDefault2(seed); Number sum = new Number().init(0); int[] indezes = new int[7]; int j = 0; while (j < 7){ indezes[j] = 0; j = j + 1; } Number[][][][][][][] arr = randomNumberArray(n, seed + 1); Number[][] vectors = randomNumberMatrix(7, n, seed + 2); while (indezes[0] < n) { while (indezes[1] < n) { while (indezes[2] < n) { while (indezes[3] < n) { while (indezes[4] < n) { while (indezes[5] < n) { while (indezes[6] < n) { Number val = new Number().init(1); int i = 0; while (i < 7){ val = val.mul(vectors[i][indezes[i]]); i = i + 1; } if (outputMatrix){ System.out.println(arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]].val); } sum = sum.add(val.mul(arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]])); indezes[6] = indezes[6] + 1; } indezes[6] = 0; indezes[5] = indezes[5] + 1; } indezes[5] = 0; indezes[4] = indezes[4] + 1; } indezes[4] = 0; indezes[3] = indezes[3] + 1; } indezes[3] = 0; indezes[2] = indezes[2] + 1; } System.out.println(sum.val); indezes[2] = 0; indezes[1] = indezes[1] + 1; } indezes[1] = 0; indezes[0] = indezes[0] + 1; } System.out.println(sum.val); } public int[][] randomMatrix(int number, int n, int seed){ LehmerRandom random = new LehmerRandom().init(seed); int[][] ret = new int[number][]; int i = 0; while (i < number){ ret[i] = new int[n]; int j = 0; while (j < n){ ret[i][j] = random.next(); j = j + 1; } i = i + 1; } return ret; } public Number[][] randomNumberMatrix(int number, int n, int seed){ LehmerRandom random = new LehmerRandom().init(seed); Number[][] ret = new Number[number][]; int i = 0; while (i < number){ ret[i] = new Number[n]; int j = 0; while (j < n){ ret[i][j] = new Number().init(random.next()); j = j + 1; } i = i + 1; } return ret; } public int[][][][][][][] randomIntArray(int n, int seed){ LCG lcg = new LCG().initWithDefault(seed); int[][][][][][][] arr = new int[n][][][][][][]; int[] indezes = new int[7]; while (indezes[0] < n) { arr[indezes[0]] = new int[n][][][][][]; while (indezes[1] < n) { arr[indezes[0]][indezes[1]] = new int[n][][][][]; while (indezes[2] < n) { arr[indezes[0]][indezes[1]][indezes[2]] = new int[n][][][]; while (indezes[3] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]] = new int[n][][]; while (indezes[4] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]] = new int[n][]; while (indezes[5] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]] = new int[n]; while (indezes[6] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]] = lcg.nextVal(); indezes[6] = indezes[6] + 1; } indezes[6] = 0; indezes[5] = indezes[5] + 1; } indezes[5] = 0; indezes[4] = indezes[4] + 1; } indezes[4] = 0; indezes[3] = indezes[3] + 1; } indezes[3] = 0; indezes[2] = indezes[2] + 1; } indezes[2] = 0; indezes[1] = indezes[1] + 1; } indezes[1] = 0; indezes[0] = indezes[0] + 1; } return arr; } public Number[][][][][][][] randomNumberArray(int n, int seed){ LCG lcg = new LCG().initWithDefault(seed); Number[][][][][][][] arr = new Number[n][][][][][][]; int[] indezes = new int[7]; while (indezes[0] < n) { arr[indezes[0]] = new Number[n][][][][][]; while (indezes[1] < n) { arr[indezes[0]][indezes[1]] = new Number[n][][][][]; while (indezes[2] < n) { arr[indezes[0]][indezes[1]][indezes[2]] = new Number[n][][][]; while (indezes[3] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]] = new Number[n][][]; while (indezes[4] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]] = new Number[n][]; while (indezes[5] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]] = new Number[n]; while (indezes[6] < n) { arr[indezes[0]][indezes[1]][indezes[2]][indezes[3]][indezes[4]][indezes[5]][indezes[6]] = new Number().init(lcg.nextRange(-1000, 1000)); indezes[6] = indezes[6] + 1; } indezes[6] = 0; indezes[5] = indezes[5] + 1; } indezes[5] = 0; indezes[4] = indezes[4] + 1; } indezes[4] = 0; indezes[3] = indezes[3] + 1; } indezes[3] = 0; indezes[2] = indezes[2] + 1; } indezes[2] = 0; indezes[1] = indezes[1] + 1; } indezes[1] = 0; indezes[0] = indezes[0] + 1; } return arr; } } class Number { public int val; public Number init(int val){ this.val = val; return this; } public Number mul(Number other){ return new Number().init(other.val * val); } public Number add(Number other){ return new Number().init(other.val + val); } }
 class Math { public int pow(int v, int exp){ if (exp < 0){ return 1 / pow(v, -exp); } else if (exp == 0){ return 1; } else { int ret = 1; while (exp > 0){ if (exp % 2 == 0){ v = v * v; exp = exp / 2; } else { ret = ret * v; exp = exp - 1; } } return ret; } } public int factorial(int val){ int ret = 1; int sign = signum(val); if (val < 0){ val = -val; } if (val == 0){ return 1; } while (val > 0){ ret = ret * val; val = val - 1; } return ret * sign; } public int min(int s, int t){ if (s < t){ return s; } return t; } public int max(int s, int t){ if (s > t){ return s; } return t; } public int lengthInChars(int num){ int len = 1; if (num < 0){ len = len + 1; num = -num; } while (num > 10){ num = num / 10; len = len + 1; } return len; } public int signum(int num){ if (num == 0){ return 0; } if (num < 0){ return -1; } return 1; } public int abs(int num){ if (num < 0){ return -num; } return num; } } /* Adapted from the following source: The Computer Language Benchmarks Game http://benchmarksgame.alioth.debian.org/ contributed by Jarkko Miettinen */ class binarytrees { public int minDepth; public static void main(String[] args){ new binarytrees().init().run(19); } public binarytrees init(){ minDepth = 4; return this; } public void run(int n){ int maxDepth = 0; if (minDepth + 2 > n){ maxDepth = minDepth + 2; } else { maxDepth = n; } int stretchDepth = maxDepth + 1; int check = bottomUpTree(0,stretchDepth).itemCheck(); /*System.out.println("stretch tree of depth "+stretchDepth+"\t check: " + check);*/ System.out.println(stretchDepth); System.out.println(check); TreeNode longLivedTree = bottomUpTree(0,maxDepth); int depth = minDepth; while (depth <= maxDepth){ int iterations = new Math().pow(2, maxDepth - depth + minDepth); check = 0; int i = 1; while (i <= iterations){ check = check + (bottomUpTree(i,depth)).itemCheck(); check = check + (bottomUpTree(-i,depth)).itemCheck(); i = i + 1; } /*System.out.println((iterations*2) + "\t trees of depth " + depth + "\t check: " + check);*/ System.out.println(iterations * 2); System.out.println(depth); System.out.println(check); depth = depth + 2; } /*System.out.println("long lived tree of depth " + maxDepth + "\t check: "+ longLivedTree.itemCheck());*/ System.out.println(maxDepth); System.out.println(longLivedTree.itemCheck()); } public TreeNode bottomUpTree(int item, int depth){ if (depth>0){ return new TreeNode().init2( bottomUpTree(2*item-1, depth-1) , bottomUpTree(2*item, depth-1) , item ); } else { return new TreeNode().init(item); } } } class TreeNode { public TreeNode left; public TreeNode right; public int item; public TreeNode init(int item){ this.item = item; return this; } public TreeNode init2(TreeNode left, TreeNode right, int item){ this.left = left; this.right = right; this.item = item; return this; } public int itemCheck(){ if (left==null) return item; else return item + left.itemCheck() - right.itemCheck(); } }
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file
 /* * conways_game_of_life.mj -- Plays Conway's Game of Life. * * This is a simulator for Conway's Game of Life [1]. To get the maximum fun * out of it, you need a video terminal that understands ANSI escape sequences. * Then, piping the output of this program through a filter as explained in * `hello_world.mj` will show you a fancy animation of the game. If you cannot * get access to a terminal (emulator) that is capable of this, you can set the * variable `HAVE_ANSI` near the beginning of `main` to `false`. Then, the * output will not use ANSI escape codes but look rather sad. You'll probably * also want to reduce the iteration count in this case. Here are all * variables that you might want to tweak. * * - `HAVE_ANSI` -- whether ANSI escape codes are supported by the terminal * - `LINES` -- number of lines available on the terminal * - `COLUMNS` -- number of columns available on the terminal * - `ITERATIONS` -- number of iterations to play * - `RANDOM_SEED` -- random seed for initializing the game * * Since MiniJava has no way to suspend, this program deliberately uses an * inefficient strategy to print the output (knowing that terminals are * *really* slow at interpreting ANSI escape codes). This means that the * program will eat all your CPU while it is running. The update rate is about * alright on my PC but if your computer is much faster or much slower, you * might not enjoy the animation very much. * * [1] https://en.wikipedia.org/wiki/Conway's_Game_of_Life * */ class CWGOLMain { public static void main(String[] args) throws Exception { boolean HAVE_ANSI = true; int LINES = 24; int COLUMNS = 80; int ITERATIONS = 50000; int RANDOM_SEED = System.in.read(); Game g = new Game(); g.init(LINES - 2, COLUMNS / 2 - 1); LCG lcg = new LCG(); lcg.init(RANDOM_SEED); int i = 0; while (i < g.rows()) { int j = 0; while (j < g.columns()) { if (lcg.next() % 8 == 0) { g.set(i, j); } j = j + 1; } i = i + 1; } BoardPrinter bp = new BoardPrinter(); bp.init(); if (HAVE_ANSI) { bp.fancyInit(g); } int k = 0; while (k < ITERATIONS) { if (k == 1 || k + 1 == ITERATIONS) { if (HAVE_ANSI) { bp.fancyPrintBoard(g, k); } else { bp.printBoard(g, k); } } g.next(); k = k + 1; } if (HAVE_ANSI) { bp.fancyFini(); } } } class BoardPrinter { public void init() { _M_digitsBuffer = new int[10]; } public void printBoard(Game game, int iteration) { _M_printInt(iteration); System.out.println(10); _M_printRule(game.columns()); int i = 0; while (i < game.rows()) { int j = 0; System.out.println(124); while (j < game.columns()) { if (game.get(i, j)) { System.out.println(42); } else { System.out.println(32); } j = j + 1; } System.out.println(124); System.out.println(10); i = i + 1; } _M_printRule(game.columns()); System.out.println(10); } public void fancyInit(Game game) { int k; _M_ansiDectcem(false); _M_ansiSgr(7, 0); _M_ansiEd(2); _M_ansiCup(1, 1); System.out.println(43); k = 0; while (k < 2 * game.columns()) { System.out.println(45); k = k + 1; } System.out.println(43); _M_ansiCup(2 + game.rows(), 1); System.out.println(43); k = 0; while (k < 2 * game.columns()) { System.out.println(45); k = k + 1; } System.out.println(43); k = 0; while (k < game.rows()) { _M_ansiCup(2 + k, 1); System.out.println(124); _M_ansiCup(2 + k, 2 + 2 * game.columns()); System.out.println(124); k = k + 1; } } public void fancyFini() { _M_ansiDectcem(true); _M_ansiSgr(9, 9); _M_ansiEd(2); _M_ansiCup(1, 1); } public void fancyPrintBoard(Game game, int iteration) { int i; int j; _M_ansiCup(1, 10); _M_ansiSgr(7, 0); _M_printInt(iteration); i = 0; while (i < game.rows()) { j = 0; while (j < game.columns()) { _M_ansiCup(2 + i, 2 + 2 * j); if (game.get(i, j)) { _M_ansiSgr(9, 2); } else { _M_ansiSgr(9, 0); } System.out.println(32); System.out.println(32); j = j + 1; } i = i + 1; } } public int[] _M_digitsBuffer; public void _M_printRule(int columns) { System.out.println(43); while (columns > 0) { System.out.println(45); columns = columns - 1; } System.out.println(43); System.out.println(10); } public void _M_ansiSgr(int fg, int bg) { _M_ansiCsi(); System.out.println(51); System.out.println(48 + fg); System.out.println(109); _M_ansiCsi(); System.out.println(52); System.out.println(48 + bg); System.out.println(109); } public void _M_ansiEd(int code) { _M_ansiCsi(); System.out.println(48 + code); System.out.println(74); } public void _M_ansiEl(int code) { _M_ansiCsi(); System.out.println(48 + code); System.out.println(75); } public void _M_ansiCup(int row, int col) { _M_ansiCsi(); _M_printInt(row); System.out.println(59); _M_printInt(col); System.out.println(72); } public void _M_ansiDectcem(boolean show) { _M_ansiCsi(); System.out.println(63); System.out.println(50); System.out.println(53); if (show) { System.out.println(104); } else { System.out.println(108); } System.out.println(10); } public void _M_ansiCsi() { System.out.println(27); System.out.println(91); } public void _M_printInt(int n) { if (n == 0) { System.out.println(48); return; } int i = 0; while (n > 0) { _M_digitsBuffer[i] = n % 10; n = n / 10; i = i + 1; } while (i > 0) { System.out.println(48 + _M_digitsBuffer[i - 1]); i = i - 1; } } } class Game { public void init(int rows, int columns) { int size = rows * columns; _M_rows = rows; _M_columns = columns; _M_active_board = new boolean[size]; _M_passive_board = new boolean[size]; int idx = 0; while (idx < size) { _M_active_board[idx] = false; _M_passive_board[idx] = false; idx = idx + 1; } } public int rows() { return _M_rows; } public int columns() { return _M_columns; } public boolean get(int i, int j) { return _M_active_board[_M_getIndex(i, j)]; } public void set(int i, int j) { _M_active_board[_M_getIndex(i, j)] = true; } public void next() { int i = 0; while (i < _M_rows) { int j = 0; while (j < _M_columns) { int index = _M_getIndex(i, j); int neighbours = _M_liveNeighbours(i, j); if ((neighbours < 2) || (neighbours > 3)) { _M_passive_board[index] = false; } else if (neighbours == 3) { _M_passive_board[index] = true; } else { _M_passive_board[index] = _M_active_board[index]; } j = j + 1; } i = i + 1; } _M_swapBuffers(); } public int _M_rows; public int _M_columns; public boolean[] _M_active_board; public boolean[] _M_passive_board; public int _M_getIndex(int i, int j) { i = (_M_rows + i) % _M_rows; j = (_M_columns + j) % _M_columns; return i * _M_columns + j; } public int _M_liveNeighbours(int i, int j) { int count = 0; if (get(i - 1, j - 1)) { count = count + 1; } if (get(i - 1, j )) { count = count + 1; } if (get(i - 1, j + 1)) { count = count + 1; } if (get(i, j - 1)) { count = count + 1; } if (get(i, j + 1)) { count = count + 1; } if (get(i + 1, j - 1)) { count = count + 1; } if (get(i + 1, j )) { count = count + 1; } if (get(i + 1, j + 1)) { count = count + 1; } return count; } public void _M_swapBuffers() { boolean[] temp = _M_active_board; _M_active_board = _M_passive_board; _M_passive_board = temp; } } class LCG { public void init(int seed) { _M_a = 5; _M_c = 11; _M_m = 7901; _M_state = seed % _M_m; } public int next() { int prev = _M_state; _M_state = (_M_a * prev + _M_c) % _M_m; return prev; } public int _M_a; public int _M_c; public int _M_m; public int _M_state; }
 class Math { public int pow(int v, int exp){ if (exp < 0){ return 1 / pow(v, -exp); } else if (exp == 0){ return 1; } else { int ret = 1; while (exp > 0){ if (exp % 2 == 0){ v = v * v; exp = exp / 2; } else { ret = ret * v; exp = exp - 1; } } return ret; } } public int factorial(int val){ int ret = 1; int sign = signum(val); if (val < 0){ val = -val; } if (val == 0){ return 1; } while (val > 0){ ret = ret * val; val = val - 1; } return ret * sign; } public int min(int s, int t){ if (s < t){ return s; } return t; } public int max(int s, int t){ if (s > t){ return s; } return t; } public int lengthInChars(int num){ int len = 1; if (num < 0){ len = len + 1; num = -num; } while (num > 10){ num = num / 10; len = len + 1; } return len; } public int signum(int num){ if (num == 0){ return 0; } if (num < 0){ return -1; } return 1; } public int abs(int num){ if (num < 0){ return -num; } return num; } } /* Adapted from the following source: The Computer Language Benchmarks Game http://shootout.alioth.debian.org/ contributed by Isaac Gouy converted to Java by Oleg Mazurov */ class fannkuchredux { public int maxFlipsCount; public void fannkuch(int n) { int[] perm = new int[n]; int[] perm1 = new int[n]; int[] count = new int[n]; maxFlipsCount = 0; int permCount = 0; int checksum = 0; int i = 0; while (i < n){ perm1[i] = i; i = i + 1; } int r = n; while (true) { while (r != 1){ count[r-1] = r; r = r - 1; } i = 0; while (i < n){ perm[i] = perm1[i]; i = i + 1; } int flipsCount = 0; int k = 0; while ( !((k=perm[0]) == 0) ) { int k2 = (k+1) / 2; int j = 0; while (j < k2){ int temp = perm[j]; perm[j] = perm[k-j]; perm[k-j] = temp; j = j + 1; } flipsCount = flipsCount + 1; } maxFlipsCount = new Math().max(maxFlipsCount, flipsCount); if (permCount % 2 == 0){ checksum = checksum + flipsCount; } else { checksum = checksum - flipsCount; } /* Use incremental change to generate another permutation */ boolean doBreak = false; while (!doBreak) { if (r == n) { System.out.println( checksum ); return; } int perm0 = perm1[0]; int l = 0; while (l < r) { int j = l + 1; perm1[l] = perm1[j]; l = j; } perm1[r] = perm0; count[r] = count[r] - 1; if (count[r] > 0) doBreak = true; else r = r + 1; } permCount = permCount + 1; } } public static void main(String[] args){ fannkuchredux fk = new fannkuchredux(); fk.fannkuch(11); System.out.println(fk.maxFlipsCount); } }
 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file
 /* Adapted from: https://en.wikipedia.org/wiki/Lehmer_random_number_generator */ class LehmerRandom { public int M; /* 2^31 - 1 (A large prime number) */ public int A; /* Prime root of M, passes statistical tests and produces a full cycle */ public int Q; /* M / A (To avoid overflow on A * seed) */ public int R; /* M % A (To avoid overflow on A * seed) */ public int seed; public LehmerRandom init(int seed){ this.M = 2147483647; this.A = 16807; this.Q = 127773; this.R = 2836; this.seed = seed; return this; } public LehmerRandom initWithDefault(){ return init(2147480677); } public int random() { int hi = seed / Q; int lo = seed % Q; int test = A * lo - R * hi; if (test <= 0) test = test + M; seed = test; return test; } public int next(){ return random(); } /** * @param min minimum number * @param max exclusive range end */ public int nextRange(int min, int max){ return next() % (max - min) + min; } public int[] intArray(int size, int min, int maxEx){ int[] arr = new int[size]; int i = 0; while (i < size){ arr[i] = nextRange(min, maxEx); i = i + 1; } return arr; } public boolean nextBoolean(){ return next() % 2 == 0; } public boolean[] booleanArray(int size){ boolean[] arr = new boolean[size]; int i = 0; while (i < size){ arr[i] = nextBoolean(); i = i + 1; } return arr; } public void shuffleIntArray(int[] arr, int size) { int i = size - 1; while (i > 0){ int index = nextRange(0, i + 1); int a = arr[index]; arr[index] = arr[i]; arr[i] = a; i = i - 1; } } } class Math { public int pow(int v, int exp){ if (exp < 0){ return 1 / pow(v, -exp); } else if (exp == 0){ return 1; } else { int ret = 1; while (exp > 0){ if (exp % 2 == 0){ v = v * v; exp = exp / 2; } else { ret = ret * v; exp = exp - 1; } } return ret; } } public int factorial(int val){ int ret = 1; int sign = signum(val); if (val < 0){ val = -val; } if (val == 0){ return 1; } while (val > 0){ ret = ret * val; val = val - 1; } return ret * sign; } public int min(int s, int t){ if (s < t){ return s; } return t; } public int max(int s, int t){ if (s > t){ return s; } return t; } public int lengthInChars(int num){ int len = 1; if (num < 0){ len = len + 1; num = -num; } while (num > 10){ num = num / 10; len = len + 1; } return len; } public int signum(int num){ if (num == 0){ return 0; } if (num < 0){ return -1; } return 1; } public int abs(int num){ if (num < 0){ return -num; } return num; } } class BooleanUtils { public int toInt(boolean value){ if (value){ return 1; } else { return 0; } } public BooleanUtils println(boolean value){ System.out.println(toInt(value)); return this; } } class ArrayUtils { public ArrayUtils printIntArray(int[] arr, int size) { int i = 0; while (i < size) { System.out.println(arr[i]); i = i + 1; } return this; } public ArrayUtils printBooleanArray(boolean[] arr, int size) { int i = 0; while (i < size) { new BooleanUtils().println(arr[i]); i = i + 1; } return this; } public int[] copyIntArray(int[] src, int start, int length) { int[] ret = new int[length]; int i = start; while (i < start + length) { ret[i - start] = src[i]; i = i + 1; } return ret; } public int toInt(int[] chars, int size) { int val = 0; size = size - 1; while (size >= 0) { val = val * 10 + chars[size] - new ASCII().init()._0; size = size - 1; } return val; } public int[] qsort(int[] a, int size) { _qsort(a, 0, size - 1); return a; } /** Adapted from http://stackoverflow.com/a/29610583 */ public void _qsort(int[] a, int left, int right) { if (right > left) { int i = left; int j = right; int tmp; int v = a[right]; boolean breakLoop = false; while (!breakLoop) { while (a[i] < v) i = i + 1; while (a[j] > v) j = j - 1; if (i <= j) { tmp = a[i]; a[i] = a[j]; a[j] = tmp; i = i + 1; j = j - 1; } if (i > j) { breakLoop = true; } } if (left < j) _qsort(a, left, j); if (i < right) _qsort(a, i, right); } } public int[] _marr; public int[] msort(int[] arr, int size) { this._marr = arr; _msort(0, size - 1); return _marr; } public void _msort(int low, int high) { if (low < high) { int mid = ((low + high) / 2); _msort(low, mid); _msort(mid + 1, high); _msort_merge(low, mid, high); } } /* Adapted from http://stackoverflow.com/a/20039399 */ public void _msort_merge(int low, int mid, int high) { int[] temp = new int[high - low + 1]; int left = low; int right = mid + 1; int index = 0; while (left <= mid && right <= high) { if (_marr[left] < this._marr[right]) { temp[index] = _marr[left]; left = left + 1; } else { temp[index] = _marr[right]; right = right + 1; } index = index + 1; } while (left <= mid || right <= high) { if (left <= mid) { temp[index] = _marr[left]; left = left + 1; index = index + 1; } else if (right <= high) { temp[index] = _marr[right]; right = right + 1; index = index + 1; } } int i = 0; while (i < high - low + 1) { _marr[low + i] = temp[i]; i = i + 1; } } } class ASCII { public int ws; public int _0; public int _9; public int lparen; public int rparen; public int plus; public int minus; public int star; /** * / */ public int slash; /** * ! */ public int exclm; /** * % */ public int mod; public ASCII init(){ this.ws = 32; lparen = 40; rparen = 41; this._0 = 48; this._9 = 57; star = 42; plus = 43; minus = 45; slash = 47; exclm = 33; mod = 37; return this; } } class Terminals { public Terminal number; public Terminal plus; public Terminal minus; public Terminal mul; public Terminal div; public Terminal eof; public Terminal pow; public Terminal lparen; public Terminal rparen; public Terminal factorial; public Terminal modulo; public Terminals init(){ this.plus = new Terminal().init(0, 6, true, true, false); this.minus = new Terminal().init(3, 6, true, true, true); this.mul = new Terminal().init(1, 7, true, true, false); this.div = new Terminal().init(2, 7, true, true, false);