package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

package recyclednumbers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		Input ip = new Input("C-small-attempt4.in");
		ip.setInputData();
		int number = 0;
		StringBuffer sb = new StringBuffer();
		for (TestCase tc : ip.getCases()){
			number++;
			sb.append("Case #" +number+": " +executeTestCase(tc).getM().size()+"\n");
		}
		Writer output = null;
		  
		  File file = new File("out.txt");
		  output = new BufferedWriter(new FileWriter(file));
		  output.write(sb.toString());
		  output.close();
	}

	private static ListRecycledNumbers executeTestCase(TestCase tc) {
		List<Integer> n = new ArrayList<Integer>();
		List<Integer> m = new ArrayList<Integer>();
		ListRecycledNumbers recycledNumbers = new ListRecycledNumbers();
		boolean recycledPairAlreadyPresent = false;
		for (int i = tc.A; i <= tc.B; i++) {
			for (int j = 1; j < Integer.toString(tc.B).length(); j++) {
				int newInt = Helper.rotateDigits(i, j, true);
				if (i != newInt) {
					if (newInt < tc.B && newInt > tc.A) {
						recycledPairAlreadyPresent = false;
						for(int k = 0; k < recycledNumbers.getM().size(); k++){
							if((recycledNumbers.getN().get(k) == newInt && recycledNumbers.getM().get(k) == i) || (recycledNumbers.getM().get(k) == newInt && recycledNumbers.getN().get(k) == i)){
								recycledPairAlreadyPresent = true;
								break;
							}
						}
						if(!recycledPairAlreadyPresent)
						recycledNumbers.add(newInt, i);
					}
				}
			}
		}
		return recycledNumbers;
		//removeRepetitiveRecycledNumbers(recycledNumbers);
	}
	/*public static ListRecycledNumbers finalList = new ListRecycledNumbers();
	private static void removeRepetitiveRecycledNumbers(
			ListRecycledNumbers recycledNumbers) {
		
		for (int i = 0; i < recycledNumbers.getM().size(); i++) {
			for (int j = 0; j < recycledNumbers.getM().size(); j++) {
				if ((recycledNumbers.getM().get(i) != recycledNumbers.getM()
						.get(j)
						&& recycledNumbers.getN().get(i) != recycledNumbers
								.getN().get(j) && i != j)
						|| (recycledNumbers.getM().get(i) != recycledNumbers
								.getN().get(j)
								&& recycledNumbers.getN().get(i) != recycledNumbers
										.getM().get(j) && i != j)) {
					finalList.add(recycledNumbers.getN().get(i), recycledNumbers.getM().get(i));
				}
			}
		}
	}
*/
}
