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

package recycled;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

/**
 *
 * @author Alfred
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    try {
            FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter outFile = new FileWriter("out.txt");
            PrintWriter out = new PrintWriter(outFile);
            String line;
            line = br.readLine();
            int cases = Integer.parseInt(line);
            for (int i=1; i<=cases;i++){
                line = br.readLine();
                String[] values = line.split(" ");
                int A = Integer.parseInt(values[0]);
                int B = Integer.parseInt(values[1]);
                int total=0;
                for (int n=A; n<=B;n++){
                    TreeSet<String> solutions = new TreeSet<String>();
                    for (int k=1;k<values[1].length();k++){
                        String m = circshift(n,k);
                        int mVal = Integer.parseInt(m);
                        if(mVal<=B && mVal!=n  && mVal> n && !m.startsWith("0") && m.length()==Integer.toString(n).length() )
                        {
                            solutions.add(m);
                        }                   
                    }
                    total+=solutions.size();
                }
                out.println("Case #"+i+": "+total);
            }
            in.close();
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

}

    private static String circshift(int n, int k) {
        String nString=Integer.toString(n);
        int len=nString.length();
        String m =  nString.substring(len-k)+nString.subSequence(0, len-k);
        //System.out.println(n+" "+m);
        return m;
    }

}

package se.round1.problem3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import se.round1.util.JamUtil;

public class Problem3 {

	Map<Character, Character> trans = new HashMap<Character, Character>();
	
	public static void main(String[] args) throws IOException{
		new Problem3();
	}
	
	public Problem3() throws IOException{
		BufferedReader reader = JamUtil.getReader("problem3/C-small-attempt0.in");
		int currCase = 0;
		int nrOfCases = Integer.parseInt(reader.readLine());
		String s = "";
		System.out.println(">>>>>>>>"+nrOfCases);
		BufferedWriter writer = JamUtil.getWriter("problem3/output.txt");
		while((s = reader.readLine()) != null){
			currCase++;
			
			int res = solveLine(s);
			String sol = "Case #"+currCase+": "+res+"\n";
			System.out.println(res);
			writer.write(sol);
		}
		writer.close();
		System.out.println("done.");
	}
	
	
	private int solveLine(String line){
		int start = Integer.parseInt(line.split(" ")[0]);
		int stop = Integer.parseInt(line.split(" ")[1]);
		Map<Integer, List<String>> map = buildMap(start, stop);
		int sum = 0;
		int rotCount = 0;
		for(int i=start;i<=stop;++i){
			String curr = Integer.toString(i);
			sum = 0;
			char[] charArray = curr.toCharArray();
			for(char c : charArray)
				sum += c;
			if(map.get(sum).size() > 1)
				rotCount += rotTryArr(curr, map.get(sum));
		}
		
		return rotCount/2;
	}
	
	private int rotTryArr(String target, List<String> list){
		int count = 0;
		for(String s : list){
			if(isSameString(s, target)){
				count++;
			}
			
		}
		return count;
	}
	
	private boolean isSameString(String s1, String s2){
		if(s1.equals(s2))
			return false;
		char[] cs1 = s1.toCharArray();
		char[] cs2 = s2.toCharArray();
		if(cs1.length != cs2.length)
			return false;
		
		for(int i=0;i<cs1.length;++i){
			if(isSame(cs1, cs2, i))
				return true;
		}
		
		return false;
	}
	
	private boolean isSame(char[] cs1, char[] cs2, int index){
		for(int i=0;i<cs1.length;++i){
			if(cs1[i] != cs2[ (i+index) % cs1.length])
				return false;
		}
		return true;
	}
	
	
	
	private Map<Integer, List<String>> buildMap(int start, int stop){
		HashMap<Integer, List<String>> m = new HashMap<Integer, List<String>>();
		for(int i=start;i<=stop;++i){
			String curr = Integer.toString(i);
			int sum = 0;
			char[] charArray = curr.toCharArray();
			for(char c : charArray)
				sum += c;
			if(m.containsKey(sum)){
				m.get(sum).add(curr);
			}
			else{
				List<String> s = new ArrayList<String>();
				s.add(curr);
				m.put(sum, s);
			}
		}
		return m;
	}
	
}
