UVa 371. Ackermann Function

The underlying concepts of UVa 371: Ackermann Functions have been discussed in great details in our post of Collatz problem. In this post, we simply outlines an ad-hoc algorithm as a solution to this problem as follows.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import java.io.PrintWriter; import java.util.Scanner; public class Main { private final Scanner in; private final PrintWriter out; private final static int _MaxValue = 1000000; private final static long[] memo = new long[_MaxValue]; public Main() { in = new Scanner(System.in); out = new PrintWriter(System.out, true); } public Main(Scanner in, PrintWriter out) { this.in = in; this.out = out; } private static long[] getInts(String input) { String[] ints = input.trim().split(" "); long[] rets = new long[2]; rets[0] = Long.parseLong(ints[0]); rets[1] = Long.parseLong(ints[1]); return rets; } private void solveAckermannProblem(long from, long to) { long maxValue = from; long maxLength = 0; for (long i = from; i <= to; i++) { long length = computeCycleLength(nextAckermannNumber(i)); if (maxLength < length) { maxValue = i; maxLength = length; } } out.println(String .format("Between %d and %d, %d generates the longest sequence of %d values.", from, to, maxValue, maxLength)); } private static long computeCycleLength(long n) { if (n == 0) return 0; if (n == 1) return 1; if (n < _MaxValue && memo[(int) n] != 0) return memo[(int) n]; long len = 1 + computeCycleLength(nextAckermannNumber(n));// computing // length of // Ackermann // sequence if (n < _MaxValue) // storing it in cache memo[(int) n] = len; return len; } public static long nextAckermannNumber(long n) { if (n % 2 == 0) return n / 2; else return n * 3 + 1; } public void run() { while (in.hasNextLine()) { long[] range = getInts(in.nextLine()); if ((range[0] == 0) && (range[1] == 0)) break; long from = Math.min(range[0], range[1]); long to = Math.max(range[0], range[1]); solveAckermannProblem(from, to); } } public static void main(String[] args) { Main solver = new Main(); solver.run(); } }
view raw UVa371.java hosted with ❤ by GitHub

Please leave a comment if you have any question regarding this problem or implementation. Thanks.

UVa 713. Adding Reversed Numbers

UVa 713: Adding Reversed Numbers is a straight-forward problem that can be solved using an ad-hoc algorithm. We have discussed a similar problem in a previous blog-post. However, this problem imposes following additional constraint—numbers will be at most 200 characters long–which necessitates the use of BigInteger. Following Java code shows such an ad-hoc algorithm that solves this problem by considering the stated constraints.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import java.io.PrintWriter; import java.math.BigInteger; import java.util.Scanner; class Main { private final Scanner in; private final PrintWriter out; public Main(){ in = new Scanner(System.in); out = new PrintWriter(System.out, true); } public Main(Scanner in, PrintWriter out){ this.in = in; this.out = out; } private static BigInteger bigIntAfterRemovingTrailingZeros(String num){ int nonZeroIndex=num.length()-1; for (; nonZeroIndex>=0; nonZeroIndex--){ if (num.charAt(nonZeroIndex) != '0'){ break; } } return new BigInteger(num.substring(0, nonZeroIndex+1)); } private static BigInteger[] readLineAsBigInts(String input){ String [] ints = input.trim().split(" "); BigInteger [] rets = new BigInteger[2]; rets[0] = bigIntAfterRemovingTrailingZeros(ints[0]); rets[1] = bigIntAfterRemovingTrailingZeros(ints[1]); return rets; } private static BigInteger reversedBigInt(BigInteger num1){ BigInteger reversedNum = BigInteger.ZERO; final int _REM = 1; final int _QUOTIENT = 0; while (num1.compareTo(BigInteger.ZERO)>0){ BigInteger [] results = num1.divideAndRemainder(BigInteger.TEN); reversedNum = reversedNum.add(results[_REM]); if(num1.compareTo(BigInteger.TEN)>=0) reversedNum =reversedNum.multiply(BigInteger.TEN); num1 = results[_QUOTIENT]; } return reversedNum; } private static BigInteger getAddRevNumbers(BigInteger num1, BigInteger num2){ return reversedBigInt(reversedBigInt(num1).add(reversedBigInt(num2))); } public void run(){ final int T = Integer.parseInt(in.nextLine().trim()); // no of test cases BigInteger [] numbers; for (int testCase = 0 ; testCase< T ; testCase++){ numbers = readLineAsBigInts(in.nextLine());// read input out.println(getAddRevNumbers(numbers[0], numbers[1]));// print result } } public static void main(String[] args) { Main uva713Solver = new Main(); uva713Solver.run(); } }
view raw uva713.java hosted with ❤ by GitHub

SPOJ 42. Adding Reversed Numbers (ADDREV) with F#.

UVa 10664. Luggage

The UVa 10664: Luggage is a typical example of the problems that can be solved using dynamic programming (DP) technique. In fact, after further analysis, this problem can be realized as a special case of Subset-Sum problem, which we have discussed in a recent post.

The following Java code snippet outlines an algorithm using dynamic programming to solve this problem. Notice that the function `solveLuggageProblem` applies a bottom-up DP to construct `dpTable`. The `boolean` value of each `dpTable[i][j]` implies that whether it is possible to achieve weight `j` from the weights of `1..i` suitcases. In this way, it determines whether `halfWeight` — the half of the total weights (of the suitcases)– can be derived from using `1..n` suitcases, i.e., whether the weights of suitcases can be distributed equally into the boots of two cars.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import java.io.PrintWriter; import java.util.Scanner; class Main { private final Scanner in; private final PrintWriter out; public Main(){ in = new Scanner(System.in); out = new PrintWriter(System.out, true); } public Main(Scanner in, PrintWriter out){ this.in = in; this.out = out; } private static int[] readLineAsIntegers(String input){ String [] ints = input.trim().split(" "); int [] rets = new int[ints.length]; for (int i = 0 ;i < ints.length; i++) rets[i] = Integer.parseInt(ints[i]); return rets; } private String solveLuggageProblem(int[] weights){ final String _TRUE = "YES"; final String _FALSE = "NO"; int totalWeight = getTotalWeight(weights); if (totalWeight%2 != 0) return _FALSE; int n = weights.length+1; int halfWeight = totalWeight/2; boolean [][] dpTable = new boolean [n][halfWeight+1]; // Base case 1: weights = 0 for all n --> True for(int i = 0;i False for(int i = 1;i
view raw uva10664.java hosted with ❤ by GitHub

Please leave a comment if you have any question regarding this problem or implementation. Thanks.

The UVa 102: Ecological Bin Packing can be solved using brute-force search, as outlined in the following Java code snippet. Note that, the primary function `getOPTConfiguration` iterates over all possible bin arrangements, and determines the arrangement that requires minimum bottle moves for a given problem instance.