/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;

/**
 *
 * @author eblanco
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JFileChooser;

public class RecycledNumbers {

    public static String DatosArchivo[] = new String[10000];

    public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
        if (sarray != null) {
            int intarray[] = new int[sarray.length];
            for (int i = 0; i < sarray.length; i++) {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
            return intarray;
        }
        return null;
    }

    public static boolean CargarArchivo(String arch) throws FileNotFoundException, IOException {
        FileInputStream arch1;
        DataInputStream arch2;
        String linea;
        int i = 0;

        if (arch == null) {
            //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
            int rc = fc.showDialog(null, "Select a File");
            if (rc == JFileChooser.APPROVE_OPTION) {
                arch = fc.getSelectedFile().getAbsolutePath();
            } else {
                System.out.println("No hay nombre de archivo..Yo!");
                return false;
            }
        }
        arch1 = new FileInputStream(arch);
        arch2 = new DataInputStream(arch1);
        do {
            linea = arch2.readLine();
            DatosArchivo[i++] = linea;
            /* if (linea != null) {
            System.out.println(linea);
            }*/
        } while (linea != null);
        arch1.close();
        return true;
    }

    public static boolean GuardaArchivo(String arch, String[] Datos) throws FileNotFoundException, IOException {
        FileOutputStream out1;
        DataOutputStream out2;
        int i;
        out1 = new FileOutputStream(arch);
        out2 = new DataOutputStream(out1);

        for (i = 0; i < Datos.length; i++) {
            if (Datos[i] != null) {
                out2.writeBytes(Datos[i] + "\n");
            }
        }
        out2.close();
        return true;
    }

    public static void echo(Object msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws IOException, Exception {
        String[] res = new String[10000], num = new String[2];
        int i, j, k, ilong;
        int ele = 1;
       ArrayList al = new ArrayList();
        String FName = "C-small-attempt0",  istr, irev, linea;
        if (CargarArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".in")) {
            for (j = 1; j <= Integer.parseInt(DatosArchivo[0]); j++) {
                linea = DatosArchivo[ele++];
                num = linea.split(" ");
                   al.clear();
                // echo("A: "+num[0]+" B: "+num[1]);
                for (i = Integer.parseInt(num[0]); i <= Integer.parseInt(num[1]); i++) {
                    istr = Integer.toString(i);
                    ilong = istr.length();
                 
                    for (k = 1; k < ilong; k++) {
                        if (ilong > k) {
                            irev = istr.substring(istr.length() - k, istr.length()) + istr.substring(0, istr.length() - k);
                            //echo("caso: " + j + ": isrt: " + istr + " irev: " + irev);
                            if ((Integer.parseInt(irev) > Integer.parseInt(num[0])) && (Integer.parseInt(irev) > Integer.parseInt(istr)) && (Integer.parseInt(irev) <= Integer.parseInt(num[1]))) {
                                al.add("(" + istr + "," + irev + ")");
                            }
                        }
                    }
                }
                HashSet hs = new HashSet();
                hs.addAll(al);
                al.clear();
                al.addAll(hs);
                res[j] = "Case #" + j + ": " + al.size();
                echo(res[j]);
                LeerArchivo.GuardaArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".out", res);
            }
        }
    }
}

package codejam2012;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

/**
 *
 * @author devashish
 */
public class ProblemC {

    private static Set<PairedNumber> pairedNumbers = new HashSet<PairedNumber>();
    private static Map<PairedNumber, Integer> pairedNumbersMap = new HashMap<PairedNumber, Integer>();

    public static void main(String[] args) {
        StringTokenizer st = null;
        BufferedReader in;
        PrintWriter out = null;

        try {
            in = new BufferedReader(new FileReader(new File("E:/c.in")));
            out = new PrintWriter(new FileWriter(new File("E:/c.out")));

            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(in.readLine());
            }

            int t = Integer.parseInt(st.nextToken());

            for (int i = 1; i <= t; i++) {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(in.readLine());
                }

                int A = Integer.parseInt(st.nextToken());
                int B = Integer.parseInt(st.nextToken());

                int outputLine = new ProblemC().getNumberOfRecycledPairs(A, B);

                out.print("Case #" + i + ": " + outputLine + "\n");
            }
        } catch (IOException e) {
        }
        out.flush();
    }

    private int getNumberOfRecycledPairs(int A, int B) {
        pairedNumbers = new HashSet<PairedNumber>();
        pairedNumbersMap = new HashMap<PairedNumber, Integer>();

        String strB = B + "";
        //System.out.println("Count from " + A + " to " + B);

        if (strB.length() == 1) {
            return 0;
        }

        for (int i = A; i <= B; i++) {
            if (isPalindrome(i)) {
                continue;
            }
            if (isSameDigits(i)) {
                continue;
            }

            PairedNumber pairedNumber = new PairedNumber(i);
            boolean added = pairedNumbers.add(pairedNumber);
            //System.out.println("i = " + i + ", added: " + added);
            if (!added) {
                int occurence = 2;
                if (pairedNumbersMap.containsKey(pairedNumber)) {
                    occurence = pairedNumbersMap.get(pairedNumber) + 1;
                }
                pairedNumbersMap.put(pairedNumber, occurence);
            }
        }

        int finalCount = 0;
        Iterator it = pairedNumbersMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            //    System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove();
            int val = (Integer) pairs.getValue();
            finalCount += (val * (val - 1)) / 2;
        }

        return finalCount;
    }

    private boolean isPalindrome(int number) {
        int reversedNumber = 0;
        while (number > 0) {
            int temp = number % 10;
            number = number / 10;
            reversedNumber = reversedNumber * 10 + temp;
        }

        if (number == reversedNumber) {
            return true;
        }

        return false;
    }

    private boolean isSameDigits(int num) {
        String str = num + "";
        char ch = str.charAt(0);
        String check = "";
        for (int i = 0; i < str.length(); i++) {
            check += ch;
        }
        if (str.equals(check)) {
            return true;
        }

        return false;
    }

    public Set<Integer> getPermutations(Integer num, int A, int B) {
        Set<Integer> perms = new HashSet<Integer>();
        int length = (num == 0) ? 1 : (int) Math.log10(num) + 1;
        String numString = num + "";
        for (int i = 1; i < length; i++) {
            String str = numString.substring(i) + numString.substring(0, i);
            //System.out.println(str);
            perms.add(Integer.parseInt(str));
        }
        //perms.add(num);

        return perms;
    }

    class PairedNumber {

        private String pairedNumber = "";

        public PairedNumber(int number) {
            this.pairedNumber = number + "";
        }

        public Set<Integer> getPermutations(Integer num) {
            Set<Integer> perms = new HashSet<Integer>();
            int length = (num == 0) ? 1 : (int) Math.log10(num) + 1;
            String numString = num + "";
            for (int i = 1; i < length; i++) {
                String str = numString.substring(i) + numString.substring(0, i);
                perms.add(Integer.parseInt(str));
            }

            return perms;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final PairedNumber other = (PairedNumber) obj;

            Set<Integer> perms = getPermutations(Integer.parseInt(this.pairedNumber));
            if (perms.contains(Integer.parseInt(other.pairedNumber))) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 13 * hash + (this.pairedNumber != null ? this.pairedNumber.length() : 0);
            return hash;
        }

        @Override
        public String toString() {
            return "PairedNumber{" + "pairedNumber=" + pairedNumber + '}';
        }
    }
}
