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

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

package com.renoux.gael.codejam.cj2012.qc;

import java.util.HashSet;
import java.util.Set;

import com.renoux.gael.codejam.fwk.Solver;
import com.renoux.gael.codejam.utils.ArrayUtils;
import com.renoux.gael.codejam.utils.Input;
import com.renoux.gael.codejam.utils.Output;

public class QC extends Solver {

	public static void main(String... args) throws Exception {
		new QC().run();
	}

	@Override
	protected void solveCase(Input in, Output out) {
		System.out.println();
		int[] line = ArrayUtils.parseInts(in.readLine().split(" "));
		int a = line[0];
		int b = line[1];

		Set<Pair> pairs = new HashSet<Pair>();
		for (int i = a; i < b; i++) {
			String number = Integer.toString(i);
			for (int k = 1; k < number.length(); k++) {
				String modified = number.substring(k) + number.substring(0, k);
				int value = Integer.parseInt(modified);
				if (i < value && value <= b) {
					// System.out.println(i + " " + value);
					Pair p = new Pair(i, value);
					if (!pairs.add(p)) {
						// System.out.println("Duplicata !!!");
					}
				}
			}

		}

		out.writeLine(pairs.size());
	}

	@Override
	protected String getProblemName() {
		return "QC";
	}

	@Override
	protected boolean disableSample() {
		return true;
	}

	@Override
	protected boolean disableLarge() {
		return true;
	}

	public static class Pair {
		public int m, n;

		public Pair(int m, int n) {
			this.m = m;
			this.n = n;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + m;
			result = prime * result + n;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Pair other = (Pair) obj;
			if (m != other.m)
				return false;
			if (n != other.n)
				return false;
			return true;
		}

	}
}
