/*
 * 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 org.digiharbor.gene;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class CJ2012 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		new CJ2012();
	}
	public CJ2012() throws FileNotFoundException, IOException {
		BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));

		String stage = "Qual";
		System.out.printf("Stage %s Problem to run: (ae, a0, a1, al, be...): ", stage);
		String cmd = sysin.readLine().toUpperCase().trim();
		char problem = cmd.charAt(0);
		String fname = "";
		if (cmd.charAt(1) == 'E')
			fname = stage + "/" + problem + "-example";
		else if (cmd.charAt(1) == 'L')
			fname = stage + "/" + problem + "-large";
		else
			fname = stage + "/" + problem + "-small-attempt" + cmd.charAt(1);

		String inf = fname + ".in";
		String outf = fname + ".out";
		BufferedReader f = new BufferedReader(new FileReader(inf));
		BufferedWriter out = new BufferedWriter(new FileWriter(outf));
		int cnt = Integer.parseInt(f.readLine());

		long start = System.currentTimeMillis();
		System.out.printf("Running %s => %s ...", inf, outf);
		switch (problem) {
		case 'A':
			new A_SpeakingInTongues(cnt, f, out);
			break;
		case 'B':
			new B_DancingWithTheGooglers(cnt, f, out);
			break;
		case 'C':
			new C_RecycledNumbers(cnt, f, out);
			break;
		case 'D':
			new D_HallOfMirrors(cnt, f, out);
			break;
		}
		long finish = System.currentTimeMillis();
		System.out.printf(" %dmsec", finish - start);
		f.close();
		out.close();
	}

	class A_SpeakingInTongues {
		public A_SpeakingInTongues(int cnt, BufferedReader infile, BufferedWriter outfile) throws IOException {
			String key = "yhesocvxduiglbkrztnwjpfmaq";
			for (int i = 0; i < cnt; i++) {
				String ln = infile.readLine();
				String res = "";
				for (int p = 0; p < ln.length(); p++) {
					if (ln.charAt(p) == ' ')
						res += ' ';
					else
						res += key.charAt(ln.charAt(p) - 'a');
				}
				outfile.write(String.format("Case #%d: %s\r\n", i + 1, res));
			}
		}
	}
	class B_DancingWithTheGooglers {
		public B_DancingWithTheGooglers(int cnt, BufferedReader infile, BufferedWriter outfile) throws IOException {
			Scanner in = new Scanner(infile);
			for (int i = 0; i < cnt; i++) {
				int nGoog = in.nextInt();
				int nSurprise = in.nextInt();
				int best = in.nextInt();

				int bestcnt = 0;
				int surprises = 0;
				int surescore = best * 3 - 2;  // i.e. >=22 ensures a best of 8 (8,7,7)
				if (surescore < 0)
					surescore = 0;
				int surprisescore = surescore - 2;  // i.e. 20,21 could get an 8 with a surprise (8,7,6) (8,6,6)
				if (surprisescore < 0)
					surprisescore = 0;
				for (int j = 0; j < nGoog; j++) {
					int total = in.nextInt();
					if (total >= best && total >= surescore)
						bestcnt++;
					else if (total >= best && total >= surprisescore)
						surprises++;
				}
				int res = bestcnt + (nSurprise >= surprises ? surprises : nSurprise);
				outfile.write(String.format("Case #%d: %d\r\n", i + 1, res));
			}
		}
	}
	class C_RecycledNumbers {
		public C_RecycledNumbers(int cnt, BufferedReader infile, BufferedWriter outfile) throws IOException {
			for (int i = 0; i < cnt; i++) {
				String[] ln = infile.readLine().trim().split(" ");
				int a = Integer.parseInt(ln[0]);
				int b = Integer.parseInt(ln[1]);
				int len = ln[0].length();
				int rescnt = 0;
				String[] rvals = new String[len];
				int rvc;
				for (int v = a; v <= b; v++) {
					String val = Integer.toString(v);
					if (val.length() < len)
						val = "0000000000".substring(0, len - val.length()) + val;
					rvc = 0;
					for (int r = 1; r < len; r++) {
						String rval = val.substring(len - r) + val.substring(0, len - r);
						int rv = Integer.parseInt(rval);
						if (rv > v && rv <= b) {
							boolean isnew = true;
							for (int k = 0; k < rvc; k++)
								if (rvals[k].equals(rval))
									isnew = false;
							if (isnew) {
								rvals[rvc++] = rval;
								rescnt++;
							} 
//							else
//								for (int k = 0; k < rvc; k++) {
//									outfile.write(String.format("%s <= %s < %s <= %s\r\n", ln[0], val, rval, ln[1]));
//								}
						}
					}
				}
				outfile.write(String.format("Case #%d: %d\r\n", i + 1, rescnt));
			}
		}
	}
	class D_HallOfMirrors {
		public D_HallOfMirrors(int cnt, BufferedReader infile, BufferedWriter outfile) throws IOException {

		}
	}
}
