/*
 * 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.util.HashSet;

public class RecycledNumbers extends Solver {

	public static void main(String...args) {
		RecycledNumbers solver = new RecycledNumbers();
//		System.out.println(solver.solve("1 9"));
//		System.out.println(solver.solve("10 40"));
//		System.out.println(solver.solve("100 500"));
//		System.out.println(solver.solve("1111 2222"));
//		System.out.println(solver.solve("1000000 2000000"));
		solver.solveSmallOf('C', 0);
//		solver.solveLargeOf('C');
	}
	
	int a, b;
	
	@Override
	public String evaluate() {
		a = in.nextInt();
		b = in.nextInt();
		
		if(a<10) return "0";
		HashSet<Signed> set = new HashSet<Signed>();
		
		for(int i=a; i<=b; i++) {
			if(set.add(new Signed(i))) {
//				System.out.println(i + " added.");
			}
		}
		
		int c = 0;
		for(Signed s: set) {
			c+=s.getAvailablePairsCount();
		}
		
		return String.valueOf(c);
	}
	
	class Signed {
		final char[] ns;
		final int sign;
		
		Signed(int n) {
			ns = String.valueOf(n).toCharArray();
			int min = Integer.MAX_VALUE;
			for(int i=0; i<ns.length; i++) {
				if(ns[0]!='0') {
					int v = Integer.parseInt(String.valueOf(ns));
					if(v<min) min = v;
				}
				shift();
			}
			sign = min;
		}
		
		private void shift() {
			char temp = ns[0];
			int i;
			for(i=0; i<ns.length-1; i++) {
				ns[i] = ns[i+1];
			}
			ns[i] = temp;
		}
		
		public int getAvailablePairsCount() {
			int size = getSize();
			size = size * (size-1) / 2;
//			System.out.println(sign + " pairs " + size);
			return size;
		}
		
		private int getSize() {
			HashSet<Integer> cases = new HashSet<Integer>();
			for(int i=0; i<ns.length; i++) {
				if(ns[0]!='0') {
					Integer value = Integer.valueOf(String.valueOf(ns));
					if(value.intValue()>=a && value.intValue()<=b)
						cases.add(value);
				}
				shift();
			}
			return cases.size();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Signed) {
				return ((Signed) obj).sign==sign;
			}
			return super.equals(obj);
		}

		@Override
		public int hashCode() {
			return sign;
		}
		
	}
	
	class Pair {
		final int n, m;
		Pair(int n, int m) {
			if(n>m) {
				this.n = m;
				this.m = n;
			} else {
				this.n = n;
				this.m = m;
			}
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Pair) {
				Pair p = (Pair) obj;
				return p.n == this.n && p.m == this.m;
			} else {
				return false;
			}
		}
		
		@Override
		public int hashCode() {
			return n*m+m;
		}
	}
}
