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

package year_2012.qualification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author paul
 * @date 14 Apr 2012
 */
public class QuestionC {

    private static Set<Integer> getCycle(int x) {

        String s = Integer.toString(x);
        Set<Integer> set = new HashSet<Integer>();
        set.add(x);
        for (int c = 1; c < s.length(); c++) {
            String t = s.substring(c).concat(s.substring(0, c));
            set.add(Integer.parseInt(t));
        }
        return set;
        
    }

    private static Set<Integer> mask(Set<Integer> set, int a, int b) {
        Set<Integer> result = new HashSet<Integer>();

        for (int x : set) {
            if (x >= a && x <= b) {
                result.add(x);
            }
        }
        
        return result;

    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        String question = "C";

//        String name = "large";
        String name = "small-attempt0";
//        String name = "test";

        String filename = String.format("%s-%s", question, name);

        BufferedReader input = new BufferedReader(
                new FileReader(String.format("/home/paul/Documents/code-jam/2012/qualification/%s.in",
                filename)));

        String firstLine = input.readLine();

        PrintWriter pw = new PrintWriter(new File(
                String.format("/home/paul/Documents/code-jam/2012/qualification/%s.out", filename)));


        int T = Integer.parseInt(firstLine);

        // Loop through test cases.
        for (int i = 0; i < T; i++) {

//             Read data.
            String[] tokens = input.readLine().split(" ");

//            int N = Integer.parseInt(input.readLine());

            int A = Integer.parseInt(tokens[0]);
            int B = Integer.parseInt(tokens[1]);

//            System.out.format("%d, %d\n", A, B);

            boolean[] used = new boolean[B+1];
            int total = 0;
            for (int n = A; n <= B; n++) {
                if (!used[n]) {
                    Set<Integer> set = mask(getCycle(n), A, B);
                    int k = set.size();
                    total += (k * (k-1))/2;
                    for (int m : set) {
                        used[m] = true;
                    }
                }
            }

            pw.format("Case #%d: %d\n", i + 1, total);
        }

        pw.close();

    }

}

package codejam2012.qualification.c;

import java.io.PrintStream;
import java.util.HashSet;
import java.util.Scanner;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

public final class GoC {

    public static void main(String[] args) throws Exception {
        String name = args[0];
        String nameIn = name + ".in";
        String nameOut = name + ".out";
        String dataIn = Resources.toString(Resources.getResource(nameIn), Charsets.ISO_8859_1);
        try (PrintStream out = new PrintStream(nameOut)) {
            run(new Scanner(dataIn), out);
        }
    }

    public static void run(Scanner in, PrintStream out) {
        new GoC(in, out).run();
    }

    private final Scanner in;
    private final PrintStream out;

    public GoC(Scanner in, PrintStream out) {
        this.in = in;
        this.out = out;
    }

    public void run() {
        int T = in.nextInt();
        for (int t = 1; t <= T; ++t) {
            int A = in.nextInt();
            int B = in.nextInt();
            out.println(String.format("Case #%d: %d", t, run(A, B)));
        }
    }

    private static class Pair {
        final int a;
        final int b;

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public static Pair of(int a, int b) {
            return new Pair(a, b);
        }

        @Override
        public String toString() {
            return "(" + a + ", " + b + ")";
        }

        public boolean valid(int A, int B) {
            String n = Integer.toString(a);
            String m = Integer.toString(b);

            return (A <= a) && (a < b) && (b <= B) && (n.length() == m.length()) && (n.charAt(0) != '0') && (m.charAt(0) != '0');
        }
    }

    private static int run(int A, int B) {
        System.out.println("A: " + A + " B: " + B);
        HashSet<Pair> done = new HashSet<>();

        for (int i = A; i <= B; ++i) {
            int[] digits = digits(i);
            int first = digits[0];

            for (int j = digits.length - 1; j > 0; --j) {
                int curr = digits[j];
                if (curr >= first) {
                    Pair pair = Pair.of(i, undigits(recycle(digits, j)));
                    if (pair.valid(A, B)) {
                        done.add(pair);
                        System.out.println(pair);
                    }
                }
            }
        }

        System.out.println(done.size());
        System.out.println();

        return done.size();
    }

    private static int[] digits(int a) {
        char[] chars = Integer.toString(a).toCharArray();
        int[] result = new int[chars.length];
        for (int i = 0; i < result.length; ++i) {
            result[i] = Character.digit(chars[i], 10);
        }
        return result;
    }

    private static int undigits(int[] digits) {
        int result = 0;
        for (int i : digits) {
            result *= 10;
            result += i;
        }
        return result;
    }

    private static int[] recycle(int[] digits, int from) {
        int[] result = new int[digits.length];
        int index = 0;
        for (int i = from; i < digits.length; ++i) {
            result[index++] = digits[i];
        }
        for (int i = 0; i < from; ++i) {
            result[index++] = digits[i];
        }
        return result;
    }
}
