/*
 * 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 codejam;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class RecycledNumbers {
	
	private StreamTokenizer in;
	private PrintWriter out;
//	private Set<Chain> map = new HashSet<Chain>();
	
	public static void main (String[] args) throws IOException {
		new RecycledNumbers().soilve();
	}
	
	int nextInt () throws IOException {
		in.nextToken();
		return (int)in.nval;
	}
	
	void soilve () throws IOException {
		in = new StreamTokenizer(new BufferedReader(new FileReader("./src/codejam/input.txt")));
		out = new PrintWriter(new FileWriter("./src/codejam/output.txt"));
		
		int t = nextInt();
		for (int i = 0; i < t; i++) {
			int cnt = 0;
			int a = nextInt();
			int b = nextInt();
			int len = Chain.getLen(a);
			for (int j = a; j <= b; j++) {
				cnt += check(a, b, j, len);
			}
			out.println("Case #" + (i + 1) + ": " + cnt);
		}
		
		out.flush();
	}
	
	int check(int a, int b, int q, int len) {
//		Chain chain = new Chain(q);
//		if (map.contains(chain)) {
//			return 0;
//		}
//		map.add(chain);
		
		int first = q;
		int cnt = 0;
		for (int i = 1; i < len; i++) {
			int last = q % 10;
			q /= 10;
			q += last * (int)Math.pow(10, len - 1);
			if ((first < q) && (a <= q) && (q <= b)) {
				cnt++;
			}
		}
		
		return cnt;
	}
	
}

class Chain {
	
	int a;
	
	Chain(int a) {
		this.a = a;
	}
	
	@Override
	public boolean equals(Object b) {
		Chain c = (Chain)b;
		int lenC = getLen(c.a);
		int lenA = getLen(a);
		if (lenA != lenC) {
			return false;
		}
		
		if (lenA == 1) {
			return a == c.a;
		}
		
		for (int i = 1; i < lenA; i++) {
			int last = c.a % 10;
			c.a /= 10;
			c.a += last * (int)Math.pow(10, lenA - 1);
			if (c.a == a) {
				return true;
			}
		}
		
		return false;
	}
	
	public static int getLen (int a) {
		int i = 1;
		int len = 0;
		while (a / i != 0) {
			len++;
			i *= 10;
		}
		return len;
	}
	
}
