import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}
package fixjava;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import fixjava.ParallelWorkQueue.CallableFactory;

public class ArrayUtils {

	// ------------------------------------------------------------------------------------------------

	/** Convert a 2D float array into a 2D double array */
	public static double[][] toDblArray(float[][] data) {
		double[][] copy = new double[data.length][data[0].length];
		for (int i = 0; i < data.length; i++)
			for (int j = 0; j < data[0].length; j++)
				copy[i][j] = data[i][j];
		return copy;
	}

	/** Convert a 2D double array into a 2D float array */
	public static float[][] toFltArray(double[][] data) {
		float[][] copy = new float[data.length][data[0].length];
		for (int i = 0; i < data.length; i++)
			for (int j = 0; j < data[0].length; j++)
				copy[i][j] = (float) data[i][j];
		return copy;
	}

	/** Transpose an array */
	public static double[][] transpose(double[][] data) {
		double[][] copy = new double[data[0].length][data.length];
		for (int i = 0; i < data.length; i++)
			for (int j = 0; j < data[0].length; j++)
				copy[j][i] = data[i][j];
		return copy;
	}

	/** Transpose an array */
	public static float[][] transpose(float[][] data) {
		float[][] copy = new float[data[0].length][data.length];
		for (int i = 0; i < data.length; i++)
			for (int j = 0; j < data[0].length; j++)
				copy[j][i] = data[i][j];
		return copy;
	}

	// ------------------------------------------------------------------------------------------------

	public static float[][] matMulPar(float[][] A, boolean transposeA, float[][] B, boolean transposeB, int numThreads) {
		return toFltArray(matMulPar(toDblArray(A), transposeA, toDblArray(B), transposeB, numThreads));
	}

	public static double[][] matMulPar(double[][] A, boolean transposeA, double[][] B, boolean transposeB, int numThreads) {
		final double[][] matA = transposeA ? transpose(A) : A;

		// Keep B transposed so that matrix multiplication is more able to exploit cache coherence (matrix is stored row-wise)
		final double[][] matBT = transposeB ? B : transpose(B);

		final int p = matA.length, q = matA[0].length, q2 = matBT[0].length, r = matBT.length;
		if (q != q2)
			throw new IllegalArgumentException("Array size mismatch");

		final double[][] result = new double[p][r];

		//final DecimalFormat formatPercent1dp = new DecimalFormat("0.0%");

		try {
			for (Future<Void> res : new ParallelWorkQueue<Integer, Void>(numThreads, ParallelWorkQueue.makeIntRangeIterable(p),
					new CallableFactory<Integer, Void>() {
						//IntegerMutable counter = new IntegerMutable();

						@Override
						public Callable<Void> newInstance(final Integer ii) {
							return new Callable<Void>() {
								int i = ii.intValue();
								double[][] resultLocal = result;
								int rLocal = r, qLocal = q;
								double[][] matALocal = matA, matBTLocal = matBT;

								@Override
								public Void call() {
									/**
									 * <code>
									 * TODO: try blocked matrix multiplication to see if it's faster
									 * 
									 *   for (int ib = 0; ib < p; ib += blockSize)
									 *     for (int jb = 0; jb < q; jb += blockSize)
									 *       for (int kb = 0; kb < r; kb += blockSize)
									 *         for (int i = ib, iMax = Math.min(p, ib + blockSize); i < iMax; i++)
									 *           for (int j = jb, jMax = Math.min(q, jb + blockSize); j < jMax; j++)
									 *             for (int k = kb, kMax = Math.min(p, kb + blockSize); k < kMax; k++)
									 *               prod[i][j] += matALocal[i][k] * matBTLocal[j][k];
									 *               
									 * N.B. the "+=" operation above needs to use TLS accumulators
									 */
									for (int j = 0; j < rLocal; j++) {
										float tot = 0.0f;
										for (int k = 0; k < qLocal; k++)
											tot += matALocal[i][k] * matBTLocal[j][k];
										resultLocal[i][j] = tot;
									}
									//							int count = counter.increment();
									//							if (count % 10 == 0)
									//								System.out.println(formatPercent1dp.format((count / (float) (pLocal - 1))));
									return null;
								}
							};
						}
					}))
				// Barricade
				res.get();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		return result;
	}

	// ------------------------------------------------------------------------------------------------

	/** Multiply A by constant b */
	public static double[][] matMulByConst(double[][] A, double b) {
		int p = A.length, q = A[0].length;
		double[][] result = new double[p][q];
		for (int i = 0; i < p; i++)
			for (int j = 0; j < q; j++)
				result[i][j] = b * A[i][j];
		return result;
	}

	/** Multiply A by constant b */
	public static float[][] matMulByConst(float[][] A, float b) {
		int p = A.length, q = A[0].length;
		float[][] result = new float[p][q];
		for (int i = 0; i < p; i++)
			for (int j = 0; j < q; j++)
				result[i][j] = b * A[i][j];
		return result;
	}

	/** Multiply A by B transpose */
	public static float[][] matMulABT(float[][] A, float[][] BT) {
		int p = A.length, q = A[0].length, q2 = BT[0].length, r = BT.length;
		if (q != q2)
			throw new IllegalArgumentException("Array size mismatch");
		float[][] result = new float[p][r];
		for (int i = 0; i < p; i++) {
			for (int j = 0; j < r; j++) {
				float tot = 0.0f;
				for (int k = 0; k < q; k++)
					// This is the main matrix multiplication routine, because both operands here
					// are in row-major order, so we get speedups of 3-8x due to cache coherence
					// on large matrices. (Use the parallel version for really large matrices...)
					tot += A[i][k] * BT[j][k];
				result[i][j] = tot;
			}
		}
		return result;
	}

	/** Multiply A by B transpose */
	public static double[][] matMulABT(double[][] A, double[][] BT) {
		int p = A.length, q = A[0].length, q2 = BT[0].length, r = BT.length;
		if (q != q2)
			throw new IllegalArgumentException("Array size mismatch");
		double[][] result = new double[p][r];
		for (int i = 0; i < p; i++) {
			for (int j = 0; j < r; j++) {
				float tot = 0.0f;
				for (int k = 0; k < q; k++)
					// This is the main matrix multiplication routine, because both operands here
					// are in row-major order, so we get speedups of 3-8x due to cache coherence
					// on large matrices. (Use the parallel version for really large matrices...)
					tot += A[i][k] * BT[j][k];
				result[i][j] = tot;
			}
		}
		return result;
	}

	/** Multiply A by B */
	public static float[][] matMulAB(float[][] A, float[][] B) {
		// Transpose B so it's row-major, and multiply by BTT==B
		return matMulABT(A, transpose(B));
	}

	/** Multiply A by B */
	public static double[][] matMulAB(double[][] A, double[][] B) {
		// Transpose B so it's row-major, and multiply by BTT==B
		return matMulABT(A, transpose(B));
	}

	/** Multiply A transpose by B */
	public static float[][] matMulATB(float[][] A, float[][] B) {
		// Transpose B so it's row-major, and multiply by BTT==B
		return matMulABT(transpose(A), transpose(B));
	}

	/** Multiply A transpose by B */
	public static double[][] matMulATB(double[][] A, double[][] B) {
		// Transpose B so it's row-major, and multiply by BTT==B
		return matMulABT(transpose(A), transpose(B));
	}

	/** Multiply A transpose by B transpose */
	public static double[][] matMulATBT(double[][] A, double[][] B) {
		return matMulABT(transpose(A), B);
	}

	/** Multiply A transpose by B transpose */
	public static float[][] matMulATBT(float[][] A, float[][] B) {
		return matMulABT(transpose(A), B);
	}

	// ------------------------------------------------------------------------------------------------

	/** Select a random subset of rows in a matrix */
	public static float[][] selectRandomRowSubset(int numRows, float[][] mat) {
		int n = Math.min(numRows, mat.length);
		float[][] rows = new float[mat.length][];
		for (int i = 0; i < mat.length; i++)
			rows[i] = mat[i];
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int j = rand.nextInt(mat.length);
			float[] tmp = rows[i];
			rows[i] = rows[j];
			rows[j] = tmp;
		}
		return Arrays.copyOf(rows, n);
	}

	// ------------------------------------------------------------------------------------------------

	/** Write a double[][] array to a file */
	public static void writeToFile(String filename, String arrayName, double[][] array) {
		try {
			System.out.println("Writing to " + filename);
			PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));

			writeArray(writer, arrayName, array);

			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	/** Write a double[][] array to a PrintWriter */
	public static void writeArray(PrintWriter writer, String name, double[][] arr) {
		writer.println(name + "\t" + arr.length + "\t" + arr[0].length);
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[0].length; j++)
				writer.print((j == 0 ? "" : "\t") + arr[i][j]);
			writer.println();
		}
	}

	/** Write a double[] array to a PrintWriter */
	public static void writeArray(PrintWriter writer, String name, double[] arr) {
		writer.println(name + "\t1\t" + arr.length);
		for (int i = 0; i < arr.length; i++)
			writer.print((i == 0 ? "" : "\t") + arr[i]);
		writer.println();
	}

}
